日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Java并发常用方法 sleep 和 wait

發(fā)布時(shí)間:2023/12/4 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java并发常用方法 sleep 和 wait 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一:sleep 和 wait

sleep()方法:

  • 功能:讓當(dāng)前線程休眠指定時(shí)間,休眠時(shí)間的準(zhǔn)確性依賴于系統(tǒng)時(shí)鐘和CPU調(diào)度機(jī)制
  • 是Thread類的靜態(tài)方法
  • 可在任何地方調(diào)用,需要處理InterruptedException
  • 當(dāng)前線程調(diào)用sleep()方法,當(dāng)前線程會進(jìn)入阻塞狀態(tài),sleep()結(jié)束進(jìn)入可執(zhí)行狀態(tài)
  • sleep()方法不釋放已獲取的鎖資源
  • wait()方法:

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

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

    二:方法簽名:

    sleep()方法是Thread類的靜態(tài)方法

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

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。