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

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

创新互联是一家以重庆网站建设公司、网页设计、品牌设计、软件运维、seo优化排名、小程序App开发等移动开发为一体互联网公司。已累计为成都除甲醛等众行业中小客户提供优质的互联网建站和软件开发服务。

 
 
 
  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多线程应用方法全解密

分享题目:Javadeadlock生成需要使用者注意源代码
当前URL:http://www.shufengxianlan.com/qtweb/news2/241952.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联