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

Java notify喚醒源代碼的經(jīng)典講例

開發(fā) 后端
Java notify喚醒在使用中如何才能更好的在使用中找到自己的地位?這就需要我們不斷的學(xué)習(xí)相關(guān)的源代碼。下面我們就來詳細(xì)的了解。

Java notify喚醒在此對象監(jiān)視器上等待的單個(gè)線程。相關(guān)的問題需要我們不斷的學(xué)習(xí),下面我們就看看如何才能更好的使用。如果所有線程都在此對象上等待,則會(huì)選擇喚醒其中一個(gè)線程。

直到當(dāng)前的線程放棄此對象上的鎖定,才能繼續(xù)執(zhí)行被喚醒的線程。此方法只應(yīng)由作為此對象監(jiān)視器的所有者的線程來調(diào)用.

"當(dāng)前的線程必須擁有此對象監(jiān)視器"與"此方法只應(yīng)由作為此對象監(jiān)視器的所有者的線程來調(diào)用"說明wait方法與notify方法必須在同步塊內(nèi)執(zhí)行,即synchronized(obj之內(nèi)).

調(diào)用對像wait方法后,當(dāng)前線程釋放對像鎖,進(jìn)入等待狀態(tài).直到其他線程(也只能是其他線程)通過Java notify喚醒方法,或 notifyAll.該線程重新獲得對像鎖.
繼續(xù)執(zhí)行,記得線程必須重新獲得對像鎖才能繼續(xù)執(zhí)行.因?yàn)閟ynchronized代碼塊內(nèi)沒有鎖是寸步不能走的.看一個(gè)很經(jīng)典的例子:

 

Java notify喚醒代碼

  1. package ProductAndConsume;   
  2. import java.util.List;   
  3. public class Consume implements Runnable{   
  4. private List container = null;   
  5. private int count;   
  6. public Consume(List lst){   
  7. this.container = lst;   
  8. }   
  9. public void run() {   
  10. while(true){   
  11. synchronized (container) {   
  12. if(container.size()== 0){   
  13. try {   
  14. container.wait();//放棄鎖   
  15. } catch (InterruptedException e) {   
  16. e.printStackTrace();   
  17. }   
  18. }   
  19. try {   
  20. Thread.sleep(100);   
  21. } catch (InterruptedException e) {   
  22. // TODO Auto-generated catch block   
  23. e.printStackTrace();   
  24. }   
  25. container.remove(0);   
  26. container.notify();   
  27. System.out.println("我吃了"+(++count)+"個(gè)");   
  28. }   
  29. }   
  30. }   
  31. }   
  32. package ProductAndConsume;   
  33. import java.util.List;   
  34. public class Product implements Runnable {   
  35. private List container = null;   
  36. private int count;   
  37. public Product(List lst) {   
  38. this.container = lst;   
  39. }   
  40. public void run() {   
  41. while (true) {   
  42. synchronized (container) {   
  43. if (container.size() > MultiThread.MAX) {   
  44. try {   
  45. container.wait();   
  46. } catch (InterruptedException e) {   
  47. e.printStackTrace();   
  48. }   
  49. }   
  50. try {   
  51. Thread.sleep(100);   
  52. } catch (InterruptedException e) {   
  53. e.printStackTrace();   
  54. }   
  55. container.add(new Object());   
  56. container.notify();   
  57. System.out.println("我生產(chǎn)了"+(++count)+"個(gè)");   
  58. }   
  59. }   
  60. }   
  61. }   
  62. package ProductAndConsume;   
  63. import java.util.ArrayList;   
  64. import java.util.List;   
  65. public class MultiThread {   
  66. private List container = new ArrayList();   
  67. public final static int MAX = 5;   
  68. public static void main(String args[]){   
  69. MultiThread m = new MultiThread();   
  70. new Thread(new Consume(m.getContainer())).start();   
  71. new Thread(new Product(m.getContainer())).start();   
  72. new Thread(new Consume(m.getContainer())).start();   
  73. new Thread(new Product(m.getContainer())).start();   
  74. }   
  75. public List getContainer() {   
  76. return container;   
  77. }   
  78. public void setContainer(List container) {   
  79. this.container = container;   
  80. }  

以上就是對Java notify喚醒相關(guān)代碼的介紹。希望大家有所幫助。

【編輯推薦】

  1. Java多線程進(jìn)程應(yīng)對同一程序運(yùn)行資源
  2. Java多線程方案如何處理關(guān)鍵代碼
  3. Java多線程操作相關(guān)問題總結(jié)
  4. Java多線程循環(huán)相關(guān)的代碼介紹
  5. Java多線程靜態(tài)數(shù)據(jù)如何進(jìn)行數(shù)據(jù)同步
責(zé)任編輯:張浩 來源: 博客園
相關(guān)推薦

2022-04-20 07:47:00

notify喚醒線程JVM

2019-03-12 11:11:25

開源Leveldb存儲(chǔ)

2011-04-22 10:43:37

JavaScript

2011-05-18 10:52:51

java編碼規(guī)范

2020-11-13 13:05:27

Java開發(fā)代碼

2011-12-15 10:10:33

Javanio

2010-03-19 16:51:53

Java Socket

2009-04-03 08:28:39

2010-03-18 14:46:18

Java SynDem

2010-03-17 19:06:59

Java join線程

2016-10-11 16:28:11

源代碼

2010-08-03 10:16:52

Flex源代碼

2010-04-29 12:57:33

Unix源代碼

2009-06-12 19:03:41

Hadoop源代碼Yahoo

2023-12-05 13:46:09

解密協(xié)程線程隊(duì)列

2010-03-19 15:02:50

Java Socket

2009-06-22 13:41:00

Java開放源代碼Sun

2010-03-17 17:11:04

Java線程通信

2011-08-02 10:13:30

Java工具

2009-03-11 11:32:10

JavaJava安全加密技術(shù)
點(diǎn)贊
收藏

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