提交 077b654a 编写于 作者: 武汉红喜's avatar 武汉红喜

concurrent test

上级 e18d67bf
package org.hongxi.java.util.concurrent;
/**
* Created by shenhongxi on 2019-08-31.
*/
public class DaemonThreadTest {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (;;) {}
});
thread.setDaemon(true);
thread.start();
System.out.println("main thread is over");
}
}
package org.hongxi.java.util.concurrent;
/**
* Created by shenhongxi on 2019-08-31.
*/
public class DeadLockTest2 {
private static Object resourceA = new Object();
private static Object resourceB = new Object();
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get ResourceA");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get ResourceB");
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get ResourceB");
}
}
});
Thread threadB = new Thread(() -> {
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get ResourceB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get ResourceA");
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get ResourceA");
}
}
});
threadA.start();
threadB.start();
}
}
package org.hongxi.java.util.concurrent;
/**
* Created by shenhongxi on 2019-08-31.
*/
public class ThreadLocalTest {
static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();
public static void main(String[] args) {
threadLocal.set("hello world");
new Thread(() -> System.out.println("thread:" + threadLocal.get())).start();
System.out.println("main:" + threadLocal.get());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册