- */
- public class JStackDemo {public static void main(String[] args) {
- Thread t1 = new Thread(new DeadLockTest(true));// 建立一个线程
- Thread t2 = new Thread(new DeadLockTest(false));// 建立另一个线程
- t1.setName("thread-dufy-1");
- t2.setName("thread-dufy-2");
- t1.start();// 启动一个线程
- t2.start();// 启动另一个线程
- }
- }
- class DeadLockTest implements Runnable {
- public boolean falg;// 控制线程
- DeadLockTest(boolean falg) {
- this.falg = falg;
- }
- public void run() {
- /**
- * 如果 falg 的值为 true 则调用 t1 线程
- */
- if (falg) {
- while (true) {
- synchronized (Demo.o1) {
- System.out.println("o1" + Thread.currentThread().getName());
- synchronized (Demo.o2) {
- System.out.println("o2" + Thread.currentThread().getName());
- }
- }
- }
- }
- /**
- * 如果 falg 的值为 false 则调用 t2 线程
- */
- else {
- while (true) {
- synchronized (Demo.o2) {
- System.out.println("o2" + Thread.currentThread().getName());
- synchronized (Demo.o1) {
- System.out.println("o1" + Thread.currentThread().getName());
- }
- }
- }
- }
- }
- }
来源: https://www.cnblogs.com/aflyun/p/9194104.html