91在线一级黄片|91视频在线观看18|成人夜间呦呦网站|91资源欧美日韩超碰|久久最新免费精品视频一区二区三区|国产探花视频在线观看|黄片真人免费三级片毛片|国产人无码视频在线|精品成人影视无码三区|久久视频爱久久免费精品

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Javanotify喚醒源代碼的經(jīng)典講例

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

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的埇橋區(qū)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

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

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

調(diào)用對(duì)像wait方法后,當(dāng)前線程釋放對(duì)像鎖,進(jìn)入等待狀態(tài).直到其他線程(也只能是其他線程)通過(guò)Java notify喚醒方法,或 notifyAll.該線程重新獲得對(duì)像鎖.
繼續(xù)執(zhí)行,記得線程必須重新獲得對(duì)像鎖才能繼續(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. }  

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


網(wǎng)站題目:Javanotify喚醒源代碼的經(jīng)典講例
文章源于:http://m.jiaoqi3.com/article/cdiggsh.html