Java deadlock生成需要使用者注意源代码

开发 后端
Java deadlock生成在使用的时候需要我们主要不少的问题,下面我们就来看看在源代码中我们能学到什么?希望大家有所收获。

Java deadlock生成需要我们注意的关键点有很多,其实有不少的问题都能在源代码中寻找到不少的答案。下面我们就看看如何才能更好的做出Java deadlock生成。希望大家有所收获。

  1. public class Deadlock extends Object ...{  
  2. private String objID;  
  3. public Deadlock(String id) ...{  
  4. objID = id;  
  5. }  
  6. public synchronized void checkOther(Deadlock other) ...{  
  7. print("entering checkOther()");  
  8. // simulate some lengthy process  
  9. try ...{  
  10. Thread.sleep(2000);  
  11. } catch (InterruptedException x) ...{  
  12. }  
  13. print("in checkOther() - about to " + "invoke 'other.action()'");  
  14. other.action();  
  15. print("leaving checkOther()");  
  16. }  
  17. public synchronized void action() ...{  
  18. print("entering action()");  
  19. // simulate some work here  
  20. try ...{  
  21. Thread.sleep(500);  
  22. } catch (InterruptedException x) ...{  
  23. }  
  24. print("leaving action()");  
  25. }  
  26. public void print(String msg) ...{  
  27. threadPrint("objID=" + objID + " - " + msg);  
  28. }  
  29. public static void threadPrint(String msg) ...{  
  30. String threadName = Thread.currentThread().getName();  
  31. System.out.println(threadName + ": " + msg);  
  32. }  
  33. public static void main(String[] args) ...{  
  34. final Deadlock obj1 = new Deadlock("obj1");  
  35. final Deadlock obj2 = new Deadlock("obj2");  
  36. Runnable runA = new Runnable() ...{  
  37. public void run() ...{  
  38. obj1.checkOther(obj2);  
  39. }  
  40. };  
  41. Thread threadA = new Thread(runA, "threadA");  
  42. threadA.start();  
  43. try ...{  
  44. Thread.sleep(200);  
  45. } catch (InterruptedException x) ...{  
  46. }  
  47. Runnable runB = new Runnable() ...{  
  48. public void run() ...{  
  49. obj2.checkOther(obj1);  
  50. }  
  51. };  
  52. Thread threadB = new Thread(runB, "threadB");  
  53. threadB.start();  
  54. try ...{  
  55. Thread.sleep(5000);  
  56. } catch (InterruptedException x) ...{  
  57. }  
  58. threadPrint("finished sleeping");  
  59. threadPrint("about to interrupt() threadA");  
  60. threadA.interrupt();  
  61. try ...{  
  62. Thread.sleep(1000);  
  63. } catch (InterruptedException x) ...{  
  64. }  
  65. threadPrint("about to interrupt() threadB");  
  66. threadB.interrupt();  
  67. try ...{  
  68. Thread.sleep(1000);  
  69. } catch (InterruptedException x) ...{  
  70. }  
  71. threadPrint("did that break the deadlock?");  
  72. }  

以上就是对Java deadlock生成的详细介绍,希望大家能有所领悟。

【编辑推荐】

  1. Java多线程中wait语句的具体使用方法
  2. Java多线程如何防止主线的阻塞
  3. Java多线程中Message类和Queue类的使用方法
  4. Java多线程程序如何掌握基本语法
  5. Java多线程应用方法全解密
责任编辑:张浩 来源: 博客园
相关推荐

2010-03-11 15:07:39

Python编程语言

2013-09-12 15:36:31

2010-05-05 14:21:37

Linux系统软件

2018-05-31 09:22:26

2023-07-27 18:39:20

低代码开发编码

2014-04-25 10:05:42

OpenStack私有云公共云

2012-12-04 09:41:00

2011-06-29 20:06:25

IT十年技术

2014-01-03 10:59:34

2015-04-16 09:44:38

苹果银联

2022-05-14 08:05:18

Linux内存管理

2009-04-20 08:31:35

GoogleAndroid移动OS

2016-08-05 12:17:58

2015-10-08 10:07:29

游戏开发内存使用

2013-10-21 10:51:01

认证管理VDI部署

2011-05-16 16:11:21

java

2009-04-07 11:24:16

Java开发注意事项

2015-11-25 13:43:56

2013-10-16 09:42:53

虚拟桌面

2011-04-01 15:09:18

Symbian诺基亚
点赞
收藏

51CTO技术栈公众号