欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java线程死锁代码详解

程序员文章站 2022-04-15 10:33:08
/** * @author hollis */public class jstackdemo { public static void main(string[] args) {...
/**
 * @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();
      }
    }

总结

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