自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
Java deadlock生成在使用的時候需要我們主要不少的問題,下面我們就來看看在源代碼中我們能學(xué)到什么?希望大家有所收獲。

Java deadlock生成需要我們注意的關(guān)鍵點有很多,其實有不少的問題都能在源代碼中尋找到不少的答案。下面我們就看看如何才能更好的做出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生成的詳細介紹,希望大家能有所領(lǐng)悟。

【編輯推薦】

  1. Java多線程中wait語句的具體使用方法
  2. Java多線程如何防止主線的阻塞
  3. Java多線程中Message類和Queue類的使用方法
  4. Java多線程程序如何掌握基本語法
  5. Java多線程應(yīng)用方法全解密
責(zé)任編輯:張浩 來源: 博客園
相關(guān)推薦

2010-03-11 15:07:39

Python編程語言

2013-09-12 15:36:31

2010-05-05 14:21:37

Linux系統(tǒng)軟件

2018-05-31 09:22:26

2023-07-27 18:39:20

低代碼開發(fā)編碼

2014-04-25 10:05:42

OpenStack私有云公共云

2012-12-04 09:41:00

2011-06-29 20:06:25

IT十年技術(shù)

2014-01-03 10:59:34

2015-04-16 09:44:38

蘋果銀聯(lián)

2024-07-30 11:13:35

Angularonpush策略

2022-05-14 08:05:18

Linux內(nèi)存管理

2009-04-20 08:31:35

GoogleAndroid移動OS

2016-08-05 12:17:58

2015-10-08 10:07:29

游戲開發(fā)內(nèi)存使用

2013-10-21 10:51:01

認證管理VDI部署

2011-05-16 16:11:21

java

2009-04-07 11:24:16

Java開發(fā)注意事項

2013-10-16 09:42:53

虛擬桌面

2015-11-25 13:43:56

點贊
收藏

51CTO技術(shù)棧公眾號