java中wait和sleep的区别
文章目錄
- Wait和sleep的區別
- 喚醒wait和sleep
java中wait和sleep的區別
在本篇文章中,我們將會討論一下java中wait()和sleep()方法的區別。并討論一下怎么使用這兩個方法。
Wait和sleep的區別
wait() 是Object中定義的native方法:
public final native void wait(long timeout) throws InterruptedException;所以每一個類的實例都可以調用這個方法。wait()只能在synchronized block中調用。它會釋放synchronized時加在object上的鎖。
sleep()是定義Thread中的native靜態類方法:
public static native void sleep(long millis) throws InterruptedException;所以Thread.sleep()可以在任何情況下調用。Thread.sleep()將會暫停當前線程,并且不會釋放任何鎖資源。
我們先看一下一個簡單的wait使用:
@Slf4j public class WaitUsage {private static Object LOCK = new Object();public static void WaitExample() throws InterruptedException {synchronized (LOCK) {LOCK.wait(1000);log.info("Object '" + LOCK + "' is woken after" +" waiting for 1 second");}} }再看一下sleep的使用:
@Slf4j public class SleepUsage {public static void sleepExample() throws InterruptedException {Thread.sleep(1000);log.info("Thread '" + Thread.currentThread().getName() +"' is woken after sleeping for 1 second");} }喚醒wait和sleep
sleep()方法自帶sleep時間,時間過后,Thread會自動被喚醒。
或者可以通過調用interrupt()方法來中斷。
相比而言wait的喚醒會比較復雜,我們需要調用notify() 和 notifyAll()方法來喚醒等待在特定wait object上的線程。
notify()會根據線程調度的機制選擇一個線程來喚醒,而notifyAll()會喚醒所有等待的線程,由這些線程重新爭奪資源鎖。
wait,notity通常用在生產者和消費者情形,我們看下怎么使用:
@Slf4j public class WaitNotifyUsage {private int count =0;public void produceMessage() throws InterruptedException {while(true) {synchronized (this) {while (count == 5) {log.info("count == 5 , wait ....");wait();}count++;log.info("produce count {}", count);notify();}}}public void consumeMessage() throws InterruptedException {while (true) {synchronized (this) {while (count == 0) {log.info("count == 0, wait ...");wait();}log.info("consume count {}", count);count--;notify();}}} }看下怎么調用:
@Testpublic void testWaitNotifyUsage() throws InterruptedException{WaitNotifyUsage waitNotifyUsage=new WaitNotifyUsage();ExecutorService executorService=Executors.newFixedThreadPool(4);executorService.submit(()-> {try {waitNotifyUsage.produceMessage();} catch (InterruptedException e) {e.printStackTrace();}});executorService.submit(()-> {try {waitNotifyUsage.consumeMessage();} catch (InterruptedException e) {e.printStackTrace();}});Thread.sleep(50000);}本文的例子可以參考https://github.com/ddean2009/learn-java-concurrency/tree/master/wait-sleep
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多教程請參考 flydean的博客
總結
以上是生活随笔為你收集整理的java中wait和sleep的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的Volatile关键字使用
- 下一篇: java中Future的使用