日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java并发常用方法 sleep 和 wait

發布時間:2023/12/4 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java并发常用方法 sleep 和 wait 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:sleep 和 wait

sleep()方法:

  • 功能:讓當前線程休眠指定時間,休眠時間的準確性依賴于系統時鐘和CPU調度機制
  • 是Thread類的靜態方法
  • 可在任何地方調用,需要處理InterruptedException
  • 當前線程調用sleep()方法,當前線程會進入阻塞狀態,sleep()結束進入可執行狀態
  • sleep()方法不釋放已獲取的鎖資源
  • wait()方法:

  • 讓當前線程進入等待狀態,當別的其他線程調用notify()或者notifyAll()方法時,當前線程進入可運行狀態
  • 是Object類的成員方法
  • wait方法必須在同步上下文中調用,例如:同步方法塊或者同步方法中,這也就意味著如果你想要調用wait方法,前提是必須獲取對象上的鎖資源
  • 當wait方法調用時,當前線程將會釋放已獲取的對象鎖資源,并進入等待隊列,其他線程就可以嘗試獲取對象上的鎖資源
  • sleep() vs wait()

    sleepwait
    同步不需要在同步方法或同步塊中調用只能在同步上下文中調用wait方法,否則或拋出IllegalMonitorStateException異常
    作用對象定義在java.lang.Thread中,作用于當前線程定義在Object類中,作用于對象本身
    釋放鎖資源
    喚醒條件超時或者調用interrupt()方法其他線程調用對象的notify()或者notifyAll()方法
    方法屬性sleep是靜態方法wait是實例方法

    二:方法簽名:

    sleep()方法是Thread類的靜態方法

    /*** 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;

    wait()方法是Object類的成員方法

    /*** 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 (&lt;condition does not hold&gt;)* 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.** @throws IllegalMonitorStateException if the current thread is not* the owner of the object's monitor.* @throws 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);}

    總結

    以上是生活随笔為你收集整理的Java并发常用方法 sleep 和 wait的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。