代码详解の多线程下的代码死锁

来源:互联网 发布:工程项目管理软件源码 编辑:程序博客网 时间:2024/05/19 02:01

一、main函数

package com.sdmjhca.springBootDemo.deadlock;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author JHMI on 2017/8/20 0020. * 此种生成死锁的方式注意下列问题: * 1、测试类内部定义的对象不能是静态的 * 2synchronized必须是嵌套锁 */public class DeadLockMain {    public static void main(String args[]) throws InterruptedException {        /*DeadLoakTest t1 = new DeadLoakTest(true);        DeadLoakTest t2 = new DeadLoakTest(false);        t1.start();        t2.start();*/        /*ExecutorService executor = Executors.newCachedThreadPool();        executor.execute(t1);        executor.execute(t2);*/        Object o1 = new String("11111111111111111");        Object o2 = new String("22222222222222");        DeadLoakTest2 test2 = new DeadLoakTest2(o1,o2);        //Thread.sleep(1000);        DeadLoakTest2 test21 = new DeadLoakTest2(o2,o1);        test2.start();        test21.start();    }}
二、两种测试类写法

package com.sdmjhca.springBootDemo.deadlock;/** * @author JHMI on 2017/8/20 0020. */public class DeadLoakTest extends Thread {    boolean flag;    public DeadLoakTest(boolean temp){        flag = temp;    }    public void run(){        if(flag){            lock(Resource.o1,Resource.o2);        }else{            lock(Resource.o2,Resource.o1);        }    }    public void lock(Object o1,Object o2){        synchronized (o1){            System.out.println("已经获得对象1的锁,"+o1);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            synchronized (o2){                System.out.println("已经获得对象2的锁,"+o2);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}
package com.sdmjhca.springBootDemo.deadlock;/** * @author JHMI on 2017/8/20 0020. * 此种生成死锁的方式注意下列问题: * 1、测试类内部定义的对象不能是静态的 * 2synchronized必须是嵌套锁 */public class DeadLoakTest2 extends Thread {    boolean flag;    //局部变量是线程私有的     Object o1;     Object o2;    /**     * 此处注意不能使用static修饰符,     */    //static静态变量和常量是线程公有的    static Object o3;    static Object o4;    public DeadLoakTest2(boolean temp){        flag = temp;    }    public DeadLoakTest2(Object o1,Object o2){        this.o1 = o1;        this.o2 = o2;    }    public void run(){        lock(o1,o2);    }    public void lock(Object o1,Object o2){        synchronized (o1){            System.out.println("已经获得对象1的锁,"+o1);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            synchronized (o2){                System.out.println("已经获得对象2的锁,"+o2);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}
package com.sdmjhca.springBootDemo.deadlock;/** * @author JHMI on 2017/8/20 0020. */public class Resource {    static final Object o1 = new String("1111");    static final Object o2 = new String("2222");}

原创粉丝点击