Java Object wait() 方法



描述

Java Object wait() 方法導致當前執行緒等待,直到另一個執行緒為此物件呼叫 notify() 方法或 notifyAll() 方法。換句話說,此方法的行為與簡單地執行呼叫 wait(0) 完全相同。

當前執行緒必須擁有此物件的監視器。執行緒釋放對該監視器的所有權並等待,直到另一個執行緒透過對 notify 方法或 notifyAll 方法的呼叫通知正在等待此物件監視器的執行緒喚醒。然後,執行緒等待它能夠重新獲得對監視器的所有權並恢復執行。

此方法只能由擁有此物件監視器的執行緒呼叫。有關執行緒如何成為監視器所有者的說明,請參閱 notify 方法。

宣告

以下是 java.lang.Object.wait() 方法的宣告

public final void wait()

引數

返回值

此方法不返回值。

異常

  • IllegalMonitorStateException − 如果當前執行緒不是物件監視器的所有者。

  • InterruptedException − 如果另一個執行緒已中斷當前執行緒。當丟擲此異常時,當前執行緒的中斷狀態將被清除。

使執行緒等待的示例

以下示例顯示了 java.lang.Object.wait() 方法的使用。在此程式中,我們建立了 ObjectDemo 類,該類具有 addElement 和 removeElement 方法。這些方法本質上是同步的,並且在新增元素時,我們使用 notifyAll() 方法來喚醒所有等待訪問物件監視器的執行緒。在 removeElement() 方法中,我們檢查列表是否為空,然後使用 wait() 方法,執行緒被置於等待狀態。在主方法中,我們有兩個可執行例項,一個用於刪除元素,另一個用於新增元素。建立並啟動執行緒後,將呼叫 addElement() 和 removeElement() 方法。

package com.tutorialspoint;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class ObjectDemo extends Object {

   private List synchedList;

   public ObjectDemo() {
      // create a new synchronized list to be used
      synchedList = Collections.synchronizedList(new LinkedList());
   }

   // method used to remove an element from the list
   public String removeElement() throws InterruptedException {
      synchronized (synchedList) {

         // while the list is empty, wait
         while (synchedList.isEmpty()) {
            System.out.println("List is empty...");
            synchedList.wait();
            System.out.println("Waiting...");
         }
         String element = (String) synchedList.remove(0);

         return element;
      }
   }

   // method to add an element in the list
   public void addElement(String element) {
      System.out.println("Opening...");
      synchronized (synchedList) {

         // add an element and notify all that an element exists
         synchedList.add(element);
         System.out.println("New Element:'" + element + "'");

         synchedList.notifyAll();
         System.out.println("notifyAll called!");
      }
      System.out.println("Closing...");
   }

   public static void main(String[] args) {
      final ObjectDemo demo = new ObjectDemo();

      Runnable runA = new Runnable() {

         public void run() {
            try {
               String item = demo.removeElement();
               System.out.println("" + item);
            } catch (InterruptedException ix) {
               System.out.println("Interrupted Exception!");
            } catch (Exception x) {
               System.out.println("Exception thrown.");
            }
         }
      };

      Runnable runB = new Runnable() {

         // run adds an element in the list and starts the loop
         public void run() {
            demo.addElement("Hello!");
         }
      };

      try {
         Thread threadA1 = new Thread(runA, "A");
         threadA1.start();

         Thread.sleep(500);

         Thread threadA2 = new Thread(runA, "B");
         threadA2.start();

         Thread.sleep(500);

         Thread threadB = new Thread(runB, "C");
         threadB.start();

         Thread.sleep(1000);

         threadA1.interrupt();
         threadA2.interrupt();
      } catch (InterruptedException x) {
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

List is empty...
List is empty...
Opening...
New Element:'Hello!'
notifyAll called!
Closing...
Waiting...
Hello!
Waiting...
List is empty...
Interrupted Exception!
java_lang_object.htm
廣告

© . All rights reserved.