java多線程之 wait(),notify(),notifyAll()

來源:互聯網
上載者:User

java多線程之 wait(),notify(),notifyAll()

wait(),notify(),notifyAll()不屬於Thread類,而是屬於Object基礎類,也就是說每個對像都有wait(),notify(),notifyAll()
的功能.因為都個對像都有鎖,鎖是每個對像的基礎,當然操作鎖的方法也是最基礎了.
先看java doc怎麼說:
 wait導致當前的線程等待,直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法。當前的線程必須擁有此對象監視器。該線程發布對此監視器的所有權並等待,直到其他線程通過調用 notify 方法,或 notifyAll 方法通知在此對象的監視器上等待的線程醒來。然後該線程將等到重新獲得對監視器的所有權後才能繼續執行.

 notify喚醒在此對象監視器上等待的單個線程。如果所有線程都在此對象上等待,則會選擇喚醒其中一個線程。直到當前的線程放棄此對象上的鎖定,才能繼續執行被喚醒的線程。此方法只應由作為此對象監視器的所有者的線程來調用.

"當前的線程必須擁有此對象監視器"與"此方法只應由作為此對象監視器的所有者的線程來調用"說明wait方法與notify方法必須在同步塊內執行,即synchronized(obj之內).

調用對像wait方法後,當前線程釋放對像鎖,進入等待狀態.直到其他線程(也只能是其他線程)通過notify 方法,或 notifyAll.該線程重新獲得對像鎖.
繼續執行,記得線程必須重新獲得對像鎖才能繼續執行.因為synchronized代碼塊內沒有鎖是寸步不能走的.看一個很經典的例子:

 

Code

package ProductAndConsume;

import java.util.List;

public class Consume implements Runnable{
    private List container = null;
    private int count;
    public Consume(List lst){
     this.container = lst;
    }
 public void run() {
  
  while(true){
   synchronized (container) {
    if(container.size()== 0){
     try {
      container.wait();//放棄鎖
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    try {
     Thread.sleep(100);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    container.remove(0);
    container.notify();
    System.out.println("我吃了"+(++count)+"個");
   }
  }
  
 }

}

package ProductAndConsume;

import java.util.List;

public class Product implements Runnable {
 private List container = null;
    private int count;
 public Product(List lst) {
  this.container = lst;
 }

 public void run() {
  while (true) {
   synchronized (container) {
    if (container.size() > MultiThread.MAX) {
     try {
      container.wait();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    try {
     Thread.sleep(100);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    container.add(new Object());
    container.notify();
    System.out.println("我生產了"+(++count)+"個");
   }
  }

 }

}

package ProductAndConsume;

import java.util.ArrayList;
import java.util.List;

public class MultiThread {
 private List container = new ArrayList();

 public final static int MAX = 5;

  public static void main(String args[]){

 MultiThread m = new MultiThread();

  new Thread(new Consume(m.getContainer())).start();
  new Thread(new Product(m.getContainer())).start();
  new Thread(new Consume(m.getContainer())).start();
  new Thread(new Product(m.getContainer())).start();
 }
 public List getContainer() {
  return container;
 }

 public void setContainer(List container) {
  this.container = container;
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.