死锁

package LESSON12;
public class Thread1 extends Thread{
    @Override
    public void run() {    
       synchronized(MyThread.lockA){
       System.out.println(getName()+"获取对象锁A");
synchronized (MyThread.lockB) {
          System.out.println(getName()+"获取对象锁B");
}
       System.out.println(getName()+"释放对象锁B");
}
     System.out.println(getName()+"释放对象锁A");
} }
package LESSON12;
public class Thread2 extends Thread{

    @Override
    public void run() {
//        try {
//            sleep(5000);//等待线程1执行完后释放锁再执行
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
synchronized(MyThread.lockB){        System.out.println(getName()+"获取对象锁B"); synchronized (MyThread.lockA) {           System.out.println(getName()+"获取对象锁A"); }
       System.out.println(getName()+"释放对象锁A");
}
      System.out.println(getName()+"释放对象锁B"); } }
package LESSON12;
class MyThread {
    final static Object lockA = new Object();
    final static Object lockB = new Object();

}
public class TestThread {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        thread1.setName("thread1");
        Thread2 thread2 = new Thread2();
        thread2.setName("thread2");    
        
        thread1.start();
        thread2.start();
    
    }

}

运行结果

虚拟灯不灭

使用单例模式:一个类只能产生唯一一个实例对象

1不允许new-------私有化构造方法

2自己new一个对象

3提供一个公开静态的get方法

package com.zy.demo07;
public class A {
    private A() {
        // TODO Auto-generated constructor stub
    }    
    private static A a=new A();//静态方法不能有非静态变量
    public static A getA(){
        return a;
    }    
}
package com.zy.demo07;
public class B {
    private B() {
    }
    private static B b=new B();
    public static B getB(){
        return b;
    }
}
package com.zy.demo07;
public class MyRunnable1 implements Runnable{
    @Override
    public void run() {
        synchronized (A.getA()) {
            System.out.println("线程1进入红色同步代码块");
            synchronized (B.getB()) {
                System.out.println("线程1进入蓝色同步代码块");    
            }
            System.out.println("线程1出了蓝色同步代码块");    
        }
        System.out.println("线程1出了红色同步代码块");    
    }
}
package com.zy.demo07;
public class MyRunnable2 implements Runnable{
    public void run() {
        synchronized (B.getB()) {
            System.out.println("线程2进入蓝色同步代码块");
            synchronized (A.getA()) {
                System.out.println("线程2进入红色同步代码块");                        
            }
            System.out.println("线程2出了红色色同步代码块");
        }
        System.out.println("线程2出了蓝色同步代码块");        
    }
}
package com.zy.demo07;
public class Test {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyRunnable1());
        Thread thread2 = new Thread(new MyRunnable2());
        thread1.start();
        thread2.start();
    }
}

结果

注:死锁并不是每一次运行时都会出现,只是说这样写可能会出现死锁。

如下图属于正常情况:线程一走完释放锁B和A后,线程二再运行就不会发生死锁现象

 死锁情况:线程一锁定A,等待线程二释放B。线程二锁定B,等待线程一释放A。

posted @ 2019-06-26 22:24  勤奋的园  阅读(142)  评论(0编辑  收藏  举报