Java线程死锁代码详解

 更新时间:2021年11月05日 10:12:19   作者:这个名字想了很久~  
本篇文章主要介绍了Java线程死锁代码详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
/**
 * @author hollis
 */
public class JStackDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(new DeadLockclass(true));//建立一个线程
        Thread t2 = new Thread(new DeadLockclass(false));//建立另一个线程
        t1.start();//启动一个线程
        t2.start();//启动另一个线程
    }
}
class DeadLockclass implements Runnable {
    public boolean falg;// 控制线程
    DeadLockclass(boolean falg) {
        this.falg = falg;
    }
    public void run() {
        /**
         * 如果falg的值为true则调用t1线程
         */
        if (falg) {
            while (true) {
                synchronized (Suo.o1) {
                    System.out.println("o1 " + Thread.currentThread().getName());
                    synchronized (Suo.o2) {
                        System.out.println("o2 " + Thread.currentThread().getName());
                    }
                }
            }
        }
        /**
         * 如果falg的值为false则调用t2线程
         */
        else {
            while (true) {
                synchronized (Suo.o2) {
                    System.out.println("o2 " + Thread.currentThread().getName());
                    synchronized (Suo.o1) {
                        System.out.println("o1 " + Thread.currentThread().getName());
                    }
                }
            }
        }
    }
}

class Suo {
    static Object o1 = new Object();
    static Object o2 = new Object();
}
import org.springframework.stereotype.Component;


@Component
public class SynchronizedTest {

      private static final Object lockA = new Object();
      private static final Object lockB = new Object();

      
      
      /**
       * ThreadA先获取lockA,在获取lockB
       */
      private static class ThreadA extends java.lang.Thread {

        @Override
        public void run() {
          // 获取临界区A
          synchronized (lockA) {
            System.out.println("get lockA success");
            // 模拟耗时操作
            try {
                Thread.currentThread().setName("线程A");
              Thread.sleep(500);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            // 获取临界区B
            synchronized (lockB) {
              System.out.println("get lockB success");
            }
          }
        }
      }

      /**
       * ThreadB先获取lockB,在获取lockA
       */
      private static class ThreadB extends java.lang.Thread {

        @Override
        public void run() {
          // 获取临界区A
          synchronized (lockB) {
            System.out.println("get lockB success");
            // 模拟耗时操作
            try {
                Thread.currentThread().setName("线程B");
              Thread.sleep(500);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            // 获取临界区B
            synchronized (lockA) {
              System.out.println("get lockA success");
            }
          }
        }
      }
      
      static {
          new ThreadA().start();
          new ThreadB().start();
      }
    }

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

最新评论