代码详解の多线程下的代码死锁
2017-08-20 20:36
309 查看
一、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、测试类内部定义的对象不能是静态的 * 2、synchronized必须是嵌套锁 */ 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、测试类内部定义的对象不能是静态的 * 2、synchronized必须是嵌套锁 */ 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"); }
相关文章推荐
- [原]Java多线程编程学习笔记之八:死锁(含代码)
- [原]Java多线程编程学习笔记之八:死锁(含代码)
- Python 爬虫多线程详解及实例代码
- 多线程详解,同步,死锁,等待-通知
- [原创] Windows Forms 实现安全的多线程详解(附带程序代码示例)
- java 多线程-锁详解及示例代码
- 多线程死锁代码实例
- Windows Forms 实现安全的多线程详解(附带程序代码示例)
- Java多线程死锁与资源限制详解
- 多线程死锁代码
- Windows Forms 实现安全的多线程详解(附带程序代码示例) [zz]
- iOS多线程GCD之队列和任务详解(实例代码验证篇)
- 用gdb调试python多线程代码-记一次死锁的发现
- 黑马程序员——java第十一、十二天:多线程(创建线程1-2、多线程同步代码、实现Runnable接口、安全死锁)
- 详解多线程的两种实现方式以及4个线程,两个线程对j加1,两个线程对j减1完整代码。
- Java编程之多线程死锁与线程间通信简单实现代码
- java多线程Thread的实现方法代码详解
- JAVA多线程死锁详解
- Python 爬虫多线程详解及实例代码
- 对于多线程编程,很多人概念不清,写代码的时候要么是处处加锁,影响性能不说,还容易莫名其妙的死锁,还有人对多线程敬而远之。