Java基礎——Java多線程中sleep()、wait()和notify()

一、sleep()


sleep()方法源碼:

   /** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param  millis *         the length of time to sleep in milliseconds * * @throws  IllegalArgumentException *          if the value of {@code millis} is negative * * @throws  InterruptedException *          if any thread has interrupted the current thread. The *          <i>interrupted status</i> of the current thread is *          cleared when this exception is thrown. */  public static native void sleep(long millis) throws InterruptedException; 

sleep()方法來自于Thread類,從源碼給出的解釋來看,sleep()方法可以做到如下幾點:

? ? ? ?(1)sleep()使當前線程進入停滯狀態(阻塞當前線程),讓出CUP的使用、目的是不讓當前線程獨自霸占該進程所獲的CPU資源,以留一定時間給其他線程執行的機會;

? ? ? ?(2)sleep()是Thread類的Static(靜態)的方法;因此他不能改變對象的機鎖,所以當在一個Synchronized塊中調用Sleep()方法時,線程雖然休眠了,但是對象的機鎖并木有被釋放,其他線程無法訪問這個對象(即使睡著也持有對象鎖)。

? ? ? ?(3)在sleep()休眠時間期滿后,該線程不一定會立即執行,這是因為其它線程可能正在運行而且沒有被調度為放棄執行,除非此線程具有更高的優先級。


代碼演示:

public class Main {  public static void main(String[] args) {  Main main = new Main();  main.startThread();  }  /** * 啟動線程 */  public void startThread() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  System.out.println("開始執行線程。。。");  System.out.println("進入睡眠狀態。。。");  try {  Thread.sleep(3000);  } catch (InterruptedException e) {  e.printStackTrace();  }  System.out.println("線程結束。。。");  }  });  t.start();  }  
}  
運行結果:
開始執行線程。。。
進入睡眠狀態。。。
線程結束。。。
從運行的結果來看,我們可以看出程序雖然在運行過程中中斷了3秒,但是在3秒結束之后依然會繼續執行代碼,直到運行結束。在睡眠的期間內,線程會一直持有monitor對象。


二、wait()


wait()方法源碼:

    /** * Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object. * In other words, this method behaves exactly as if it simply * performs the call {@code wait(0)}. * <p> * The current thread must own this object's monitor. The thread * releases ownership of this monitor and waits until another thread * notifies threads waiting on this object's monitor to wake up * either through a call to the {@code notify} method or the * {@code notifyAll} method. The thread then waits until it can * re-obtain ownership of the monitor and resumes execution. * <p> * As in the one argument version, interrupts and spurious wakeups are * possible, and this method should always be used in a loop: * <pre> *     synchronized (obj) { *         while (<condition does not hold>) *             obj.wait(); *         ... // Perform action appropriate to condition *     } * </pre> * This method should only be called by a thread that is the owner * of this object's monitor. See the {@code notify} method for a * description of the ways in which a thread can become the owner of * a monitor. * * @exception  IllegalMonitorStateException  if the current thread is not *               the owner of the object's monitor. * @exception  InterruptedException if any thread interrupted the *             current thread before or while the current thread *             was waiting for a notification.  The <i>interrupted *             status</i> of the current thread is cleared when *             this exception is thrown. * @see        java.lang.Object#notify() * @see        java.lang.Object#notifyAll() */  public final void wait() throws InterruptedException {  wait(0);  } 

首先wait()是屬于Object類的方法,從源碼給出的解釋來看,wait()方法可以做到如下幾點:

? ? ? ?(1)wait()方法是Object類里的方法;當一個線程執行到wait()方法時,它就進入到一個和該對象相關的等待池中,同時失去(釋放)了對象的機鎖(暫時失去機鎖,wait(long timeout)超時時間到后還需要返還對象鎖);其他線程可以訪問;

? ? ? ?(2)每個線程必須持有該對象的monitor。如果在當前線程中調用wait()方法之后,該線程就會釋放monitor的持有對象并讓自己處于等待狀態。

? ? ? ?(3)如果想喚醒一個正在等待的線程,那么需要開啟一個線程通過notify()或者notifyAll()方法去通知正在等待的線程獲取monitor對象。如此,該線程即可打破等待的狀態繼續執行代碼。

? ? ? ?(4)wiat()必須放在synchronized block中,否則會在program runtime時扔出”java.lang.IllegalMonitorStateException“異常。


代碼演示:

public class Main {  public static void main(String[] args) {  Main main = new Main();  main.startThread();  }  /** * 線程鎖 */  private final Object object = new Object();  /** * 啟動線程 */  public void startThread() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  System.out.println("開始執行線程。。。");  System.out.println("進入等待狀態。。。");  synchronized (object) {  try {  object.wait();  } catch (InterruptedException e) {  e.printStackTrace();  }  }  System.out.println("線程結束。。。");  }  });  t.start();  }  
}  

? ? ? ?從代碼來看,在執行線程和線程結束之間,我們先讓該線程獲取object對象作為自己的object's monitor,然后調用了object對象的wait()方法從而讓其進入等待狀態。那么程序運行的結果如下:

開始執行線程。。。
進入等待狀態。。。
程序在未被喚醒之后,將不再打印“線程結束”,并且程序無法執行完畢一直處于等待狀態。


那么從以上的理論和實踐來分析,我們能得出如下結論:

? ? ? ?(1)在線程的運行過程中,調用該線程持有monitor對象的wait()方法時,該線程首先會進入等待狀態,并將自己持有的monitor對象釋放。

? ? ? ?(2)如果一個線程正處于等待狀態時,那么喚醒它的辦法就是開啟一個新的線程,通過notify()或者notifyAll()的方式去喚醒。當然,需要注意的一點就是,必須是同一個monitor對象。

? ? ? ?(3)sleep()方法雖然會使線程中斷,但是不會將自己的monitor對象釋放,在中斷結束后,依然能夠保持代碼繼續執行。


三、notify()和notifyAll()


說完了sleep()和wait()方法之后,我們接下來討論一下Object類中的另外兩個與wait()相關的方法。首先還是通過源碼的方式讓大家先初步了解一下:

    /** * Wakes up a single thread that is waiting on this object's * monitor. If any threads are waiting on this object, one of them * is chosen to be awakened. The choice is arbitrary and occurs at * the discretion of the implementation. A thread waits on an object's * monitor by calling one of the {@code wait} methods. * <p> * The awakened thread will not be able to proceed until the current * thread relinquishes the lock on this object. The awakened thread will * compete in the usual manner with any other threads that might be * actively competing to synchronize on this object; for example, the * awakened thread enjoys no reliable privilege or disadvantage in being * the next thread to lock this object. * <p> * This method should only be called by a thread that is the owner * of this object's monitor. A thread becomes the owner of the * object's monitor in one of three ways: * <ul> * <li>By executing a synchronized instance method of that object. * <li>By executing the body of a {@code synchronized} statement *     that synchronizes on the object. * <li>For objects of type {@code Class,} by executing a *     synchronized static method of that class. * </ul> * <p> * Only one thread at a time can own an object's monitor. * * @exception  IllegalMonitorStateException  if the current thread is not *               the owner of this object's monitor. * @see        java.lang.Object#notifyAll() * @see        java.lang.Object#wait() */  public final native void notify(); 

先來看下notify()這個方法,通過閱讀源碼我們可以總結一下幾點:
(1)當一個線程處于wait()狀態時,也即等待它之前所持有的object's monitor被釋放,通過notify()方法可以讓該線程重新處于活動狀態,從而去搶奪object's monitor,喚醒該線程。
? ? ? ?(2)如果多個線程同時處于等待狀態,那么調用notify()方法只能隨機喚醒一個線程。
? ? ? ?(3)在同一時間內,只有一個線程能夠獲得object's monitor,執行完畢之后,則再將其釋放供其它線程搶占。
當然,如何使線程成為object‘s monitor的持有者,我會在多線程的其他博客中講解。

接下來,我們再來看看notifyAll()方法:
    /** * Wakes up all threads that are waiting on this object's monitor. A * thread waits on an object's monitor by calling one of the * {@code wait} methods. * <p> * The awakened threads will not be able to proceed until the current * thread relinquishes the lock on this object. The awakened threads * will compete in the usual manner with any other threads that might * be actively competing to synchronize on this object; for example, * the awakened threads enjoy no reliable privilege or disadvantage in * being the next thread to lock this object. * <p> * This method should only be called by a thread that is the owner * of this object's monitor. See the {@code notify} method for a * description of the ways in which a thread can become the owner of * a monitor. * * @exception  IllegalMonitorStateException  if the current thread is not *               the owner of this object's monitor. * @see        java.lang.Object#notify() * @see        java.lang.Object#wait() */  public final native void notifyAll();  
那么顧名思義,notifyAll()就是用來喚醒正在等待狀態中的所有線程的,不過也需要注意以下幾點:
(1)notifyAll()只會喚醒那些等待搶占指定object's monitor的線程,其他線程則不會被喚醒。
? ? ? ?(2)notifyAll()只會一個一個的喚醒,而并非統一喚醒。因為在同一時間內,只有一個線程能夠持有object's monitor
? ? ? ?(3)notifyAll()只是隨機的喚醒線程,并非有序喚醒。
那么如何做到有序喚醒是我們接下來要討論的問題。

notify()實現有序喚醒的思路和實現

就上節提出的問題,我們在這節中可以進行一下思考和討論。
首先,簡單來說,我們需要去解決的其實就是對于多線程針對object's monitor的有序化。那么根據這一思路,我直接上代碼:
public class MyThreadFactory {  // 線程A是否處于等待狀態的標志  private boolean isThreadAWaiting;  // 線程B是否處于等待狀態的標志  private boolean isThreadBWaiting;  // 線程C是否處于等待狀態的標志  private boolean isThreadCWaiting;  public MyThreadFactory() {  isThreadAWaiting = true;  isThreadBWaiting = true;  isThreadCWaiting = true;  }  /** * 對象鎖 */  private final Object object = new Object();  /** * 該線程作為一個喚醒線程 */  public void startWakenThread() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  synchronized (object) {  System.out.println("喚醒線程開始執行...");  // 首先釋放線程A  quitThreadA();  }  }  });  t.start();  }  /** * 啟動線程A */  public void startThreadA() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  synchronized (object) {  System.out.println("線程A開始等待...");  try {  for (; ; ) {  if (!isThreadAWaiting) break;  object.wait();  }  } catch (InterruptedException e) {  e.printStackTrace();  }  System.out.println("線程A結束...");  // 線程A結束后,暫停2秒釋放線程B  try {  Thread.sleep(2000);  } catch (InterruptedException e) {  e.printStackTrace();  }  quitThreadB();  }  }  });  t.start();  }  /** * 啟動線程B */  public void startThreadB() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  synchronized (object) {  System.out.println("線程B開始等待...");  try {  for (; ; ) {  if (!isThreadBWaiting) break;  object.wait();  }  } catch (InterruptedException e) {  e.printStackTrace();  }  System.out.println("線程B結束...");  // 線程B結束后,暫停2秒釋放線程C  try {  Thread.sleep(2000);  } catch (InterruptedException e) {  e.printStackTrace();  }  quitThreadC();  }  }  });  t.start();  }  /** * 啟動線程C */  public void startThreadC() {  Thread t = new Thread(new Runnable() {  @Override  public void run() {  synchronized (object) {  System.out.println("線程C開始等待...");  try {  for (; ; ) {  if (!isThreadCWaiting) break;  object.wait();  }  } catch (InterruptedException e) {  e.printStackTrace();  }  System.out.println("線程C結束...");  try {  Thread.sleep(1000);  } catch (InterruptedException e) {  e.printStackTrace();  }  System.out.println("所有線程執行完畢!");  }  }  });  t.start();  }  /** * 線程A退出等待 */  private void quitThreadA() {  isThreadAWaiting = false;  object.notify();  }  /** * 線程B退出等待 */  private void quitThreadB() {  isThreadBWaiting = false;  object.notify();  }  /** * 線程C退出等待 */  private void quitThreadC() {  isThreadCWaiting = false;  object.notify();  }  
} 
在以上代碼中,我寫了三個線程A,B,C用來作為等待線程,并且最后通過一個喚醒線程來喚醒這三個線程。
我的思路是這樣的:
(1)通過notify()方法可以保證每次只喚醒一個線程,但是不能確保喚醒的是哪個線程。
(2)在線程A,B,C中,添加for或者while循環的方式使其進入無限等待的狀態。這樣能夠保證notify()無論如何都不能喚醒線程。
(3)分別給A,B,C線程設置各自的標記,如果要喚醒該線程的話,就改變其狀態并且跳出死循環,在最后執行下一個線程。

那么最終調用的main函數如下:
    public static void main(String[] args) {  MyThreadFactory factory = new MyThreadFactory();  factory.startThreadA();  factory.startThreadB();  factory.startThreadC();  try {  Thread.sleep(3000);  } catch (InterruptedException e) {  e.printStackTrace();  }  factory.startWakenThread();  }
運行結果:
線程A開始等待...
線程B開始等待...
線程C開始等待...
喚醒線程開始執行...
線程A結束...
線程B結束...
線程C結束...
所有線程執行完畢...

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/447000.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/447000.shtml
英文地址,請注明出處:http://en.pswp.cn/news/447000.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Key_handle的學習

代碼 一切盡在不言中 #pragma once#include "common/common.h" #include "sdf/sdf.h"#include <memory>namespace sdf {namespace algorithm {class KeyHandle {public:using erased_internal_data_t char; //使用erased_internal_data_t等效于ch…

Java基礎——虛擬機結構

一、Java平臺結構圖二、JVM、JRE和JDK關系JVM&#xff1a;Java Virtual Machine&#xff08;Java虛擬機&#xff09;&#xff0c;負責執行符合規范的Class文件 JRE&#xff1a; Java Runtime Environment &#xff08;java運行環境&#xff09;&#xff0c;包含JVM和類庫 JDK&a…

解決 SSH Connection closed by foreign host 問題

用 Xshell 連接服務器總是報錯 : Connection closed by foreign host.Disconnected from remote host... 原因可能是 SSH 服務器沒設置保活時間間隔 , 具體設置如下 : 操作 # vim /etc/ssh/sshd_config 添加兩行 , 或去掉注釋 : ClientAliveInterval 60ClientAliveCountMax…

Java基礎——synchronized

synchronized重要&#xff01;重要&#xff01;重要&#xff01;重要的事情說三遍&#xff0c;一定要記下來哦。 Java語言的關鍵字&#xff0c;當它用來修飾一個方法或者一個代碼塊的時候&#xff0c;能夠保證在同一時刻最多只有一個線程執行該段代碼。一、當兩個并發線程訪問同…

C++:MAC安裝Boost庫文件并且使用CLion開發

boost的filestem庫 C在17版本的標準庫中引入了一個filesystem庫&#xff0c;用來處理文件路徑&#xff0c;以及文件訪問。很多編譯器對filesystem庫的支持還不是很好。為了解決這個問題&#xff0c;可以臨時使用boost::filesystem來替代。其實C17標準中的filesystem庫就是從bo…

Java基礎——Java異常處理機制

一、引言 try…catch…finally恐怕是大家再熟悉不過的語句了&#xff0c;而且感覺用起來也是很簡單&#xff0c;邏輯上似乎也是很容易理解。不過&#xff0c;我親自體驗的“教訓”告訴我&#xff0c;這個東西可不是想象中的那么簡單、聽話。不信&#xff1f;那你看看下面的代碼…

clion在使用sqlite3的時候,顯示Undefined symbols for architecture x86_64錯誤的解決辦法

顯示Undefined symbols for architecture x86_64錯誤的原因 1、缺少靜態庫 環境&#xff1a;在模擬器上報錯但在真機上能運行成功&#xff0c;而且報的錯誤來自于第三方庫。原因&#xff1a;architecture x86_64 是指模擬器的架構&#xff0c;意思就是 Crypto 變量在模擬器架…

Java基礎——Java反射機制及IoC原理

一、概念 主要是指程序可以訪問&#xff0c;檢測和修改它本身狀態或行為的一種能力&#xff0c;并能根據自身行為的狀態和結果&#xff0c;調整或修改應用所描述行為的狀態和相關的語義。在java中&#xff0c;只要給定類的名字&#xff0c; 那么就可以通過反射機制來獲得類的所…

Ubuntu boost庫文件安裝編譯

簡單介紹 Boost庫是為C語言標準庫提供擴展的一些C程序庫的總稱&#xff0c;由Boost社區組織開發、維護.Boost向來有準標準庫之稱&#xff0c;很多新特性例如智能指針等都是先在boost中實現&#xff0c;后來被吸收到標準庫之中. Boost實現了日志、算法、日期、地理、數學、線程…

Java基礎——類加載機制及原理

一、什么是類的加載&#xff1f; 類的加載指的是將類的.class文件中的二進制數據讀入到內存中&#xff0c;將其放在運行時數據區的方法區內&#xff0c;然后在堆區創建一個java.lang.Class對象&#xff0c;用來封裝類在方法區內的數據結構。類的加載的最終產品是位于堆區中的Cl…

在Ubuntu環境下使用vcpkg安裝sqlite_orm包文件

Ubuntu安裝vcpkg 從github下載vcpkg的安裝包&#xff0c;在usr/local路徑下面執行如下命令 git clone https://github.com/Microsoft/vcpkg.git cd vcpkg //進入源碼目錄 ./bootstrap-vcpkg.sh //執行./bootstrap-vcpkg.sh進行編譯安裝&#xff0c;這個過程很慢 編譯安裝好…

finally語句與return語句的執行順序

網上有很多人探討Java中異常捕獲機制try...catch...finally塊中的finally語句是不是一定會被執行&#xff1f;很多人都說不是&#xff0c;當然他們的回答是正確的&#xff0c;經過我試驗&#xff0c;至少有兩種情況下finally語句是不會被執行的&#xff1a; try語句沒有被執行到…

window電腦查看ssh公鑰,以及將自己的公鑰添加到Github等類似網站

查看本機的ssh公鑰 使用命令 cd ~/.ssh使用命令 ls 可以看到 id_rsa id_rsa.pub known_hosts 三個文件&#xff0c;此處需要的是id_rsa.pub文件使用命令 cat id_rsa.pub 查看文件的內容拷貝這段內容 添加自己的公鑰 進入賬戶的設置頁面參照如下步驟&#xff0c;進入SSH Key…

java八大排序算法

一、概述 排序有內部排序和外部排序&#xff0c;內部排序是數據記錄在內存中進行排序&#xff0c;而外部排序是因排序的數據很大&#xff0c;一次不能容納全部的排序記錄&#xff0c;在排序過程中需要訪問外存。 我們這里說說八大排序就是內部排序。 當n較大&#xff0c;則應采…

密鑰安全性討論之密鑰分層管理結構

密鑰分層管理結構 密鑰的安全管理通常采用層次化的保護方法。密鑰管理分層管理機制將密鑰分為三層&#xff0c;即根密鑰、密鑰加密密鑰和工作密鑰下層密鑰為上層密鑰提供加密保護&#xff0c;采用分層的密鑰結構有助于密鑰的管理滿足本規范的要求 工作密鑰 工作密鑰對本地保存…

windows安裝 Git Large File Storage大文件下載工具ge

下載地址 導航到 git-lfs.github.com 并單擊Download開始下載git-lfs的用法指南 驗證安裝成功 打開Git Bash驗證安裝成功&#xff0c;使用命令 git lfs install &#xff0c;如果出現 >Git LFS initlized&#xff0c;就代表安裝成功參考鏈接 安裝 Git Large File Storag…

Java基礎——volatile關鍵字解析

簡介volatile關鍵字雖然從字面上理解起來比較簡單&#xff0c;但是要用好不是一件容易的事情。由于volatile關鍵字是與Java的內存模型有關的&#xff0c;因此在講述volatile關鍵之前&#xff0c;我們先來了解一下與內存模型相關的概念和知識&#xff0c;然后分析了volatile關鍵…

Linux ubuntu對于cmake的版本更新

問題產生 在ubuntu環境下運行C代碼&#xff0c;工程文件中CMakeLists文件顯示要求cmake的版本最低是3.15&#xff0c;但是我的本地版本是3.11&#xff0c;雖然修改CMakelists文件為3.11也是可以編譯通過&#xff0c;但是潛在的問題是未知的。 查看本地cmake的版本 cmake --ve…

Java基礎——Java IO詳解

一、概述 1、Java IO Java IO即Java 輸入輸出系統。不管我們編寫何種應用&#xff0c;都難免和各種輸入輸出相關的媒介打交道&#xff0c;其實和媒介進行IO的過程是十分復雜的&#xff0c;這要考慮的因素特別多&#xff0c;比如我們要考慮和哪種媒介進行IO&#xff08;文件、控…