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

Java多線程語句具體分類的詳細(xì)介紹

開發(fā) 后端
Java多線程語句需要我們熟悉很多小語法的應(yīng)用。下面我悶酒來看看wait(),notify(),notifyAll的相關(guān)語法和應(yīng)用。

Java多線程語句中有很多的小的語句需要我們特殊的注意。wait(),notify(),notifyAll()不屬于Thread類,下面我們就來詳細(xì)的看看如何使用這幾個(gè)分類代碼。希望大家有所收獲。

而是屬于Object基礎(chǔ)類,也就是說每個(gè)對(duì)像都有wait(),notify(),notifyAll()的功能.因?yàn)槎紓€(gè)對(duì)像都有鎖,鎖是每個(gè)對(duì)像的基礎(chǔ),當(dāng)然操作鎖的方法也是最基礎(chǔ)了.先看java doc怎么說:

Java多線程語句中,wait導(dǎo)致當(dāng)前的線程等待,直到其他線程調(diào)用此對(duì)象的 notify() 方法或 notifyAll() 方法。當(dāng)前的線程必須擁有此對(duì)象監(jiān)視器。該線程發(fā)布對(duì)此監(jiān)視器的所有權(quán)并等待,直到其他線程通過調(diào)用 notify 方法,或 notifyAll 方法通知在此對(duì)象的監(jiān)視器上等待的線程醒來。然后該線程將等到重新獲得對(duì)監(jiān)視器的所有權(quán)后才能繼續(xù)執(zhí)行.

notify喚醒在此對(duì)象監(jiān)視器上等待的單個(gè)線程。如果所有線程都在此對(duì)象上等待,則會(huì)選擇喚醒其中一個(gè)線程。直到當(dāng)前的線程放棄此對(duì)象上的鎖定,才能繼續(xù)執(zhí)行被喚醒的線程。此方法只應(yīng)由作為此對(duì)象監(jiān)視器的所有者的線程來調(diào)用.

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

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

 

 

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

 

以上就是對(duì)Java多線程語句的詳細(xì)介紹。

【編輯推薦】

  1. 2010年將是Java模塊化的一年
  2. Hibernate之父建議開發(fā)者升級(jí)到Java EE 6
  3. Java 6 u18更新細(xì)節(jié) Hotspot性能大增
  4. 淺析C++函數(shù)參數(shù)與Java傳遞比較
  5. Java動(dòng)態(tài)模塊化運(yùn)行原理與實(shí)踐
責(zé)任編輯:張浩 來源: CSDN
相關(guān)推薦

2011-07-22 14:55:20

多線程

2010-03-10 19:25:04

python多線程

2010-03-15 18:18:33

Java多線程

2010-03-15 19:37:00

Java多線程同步

2010-03-17 19:24:38

Java多線程循環(huán)

2010-03-17 15:45:06

Java多線程求和

2010-03-18 15:47:07

Java創(chuàng)建線程

2023-03-31 14:15:57

SQLORDER BY

2023-10-06 23:06:01

多線程Python

2010-03-10 18:32:45

Python多線程

2011-04-18 09:22:38

多線程

2011-07-22 17:35:17

java路徑

2009-08-20 15:26:42

C#循環(huán)語句

2010-03-17 17:30:26

JAVA多線程實(shí)現(xiàn)

2009-12-01 13:41:49

靜態(tài)路由設(shè)置

2010-09-07 14:36:24

SQL語句

2022-03-09 17:01:32

Python多線程多進(jìn)程

2010-03-15 15:02:22

Python type

2010-01-18 14:09:58

C++多線程

2010-03-17 17:54:25

java Socket
點(diǎn)贊
收藏

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