最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java如何讓線程主動(dòng)讓出 CPU

 更新時(shí)間:2026年05月12日 08:37:16   作者:ybwycx  
本文主要介紹了Java如何讓線程主動(dòng)讓出 CPU,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Thread.sleep

sleep 方法可以讓線程主動(dòng)讓出 CPU,但是并不會(huì)釋放鎖。

    /**
     * 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.
     */
    public static native void sleep(long millis) throws InterruptedException;

Thread.yield

yield 也可以讓線程主動(dòng)讓出 CPU,然后和其他線程一起競(jìng)爭(zhēng) CPU,但是調(diào)度器也可以忽略 yield。哪些情況會(huì)用到 yield 呢?
1. 一般在 debug 和 test 中使用。
2. CPU 密集型應(yīng)用主動(dòng)讓出 CPU 以避免過(guò)度占用 CPU,影響其他任務(wù)。

    /**
     * A hint to the scheduler that the current thread is willing to yield
     * its current use of a processor. The scheduler is free to ignore this
     * hint.
     *
     * <p> Yield is a heuristic attempt to improve relative progression
     * between threads that would otherwise over-utilise a CPU. Its use
     * should be combined with detailed profiling and benchmarking to
     * ensure that it actually has the desired effect.
     *
     * <p> It is rarely appropriate to use this method. It may be useful
     * for debugging or testing purposes, where it may help to reproduce
     * bugs due to race conditions. It may also be useful when designing
     * concurrency control constructs such as the ones in the
     * {@link java.util.concurrent.locks} package.
     */
    public static native void yield();

Thread.currentThread().suspend()

該方法已過(guò)時(shí)。為啥呢?suspend 掛起線程,并不會(huì)釋放鎖,又不像 sleep 那樣一段時(shí)間后自動(dòng)恢復(fù),所以容易引起死鎖。相對(duì)應(yīng)的 resume 方法用于喚醒一個(gè) suspend 的線程。

    /**
     * Suspends this thread.
     * <p>
     * First, the <code>checkAccess</code> method of this thread is called
     * with no arguments. This may result in throwing a
     * <code>SecurityException </code>(in the current thread).
     * <p>
     * If the thread is alive, it is suspended and makes no further
     * progress unless and until it is resumed.
     *
     * @exception  SecurityException  if the current thread cannot modify
     *               this thread.
     * @see #checkAccess
     * @deprecated   This method has been deprecated, as it is
     *   inherently deadlock-prone.  If the target thread holds a lock on the
     *   monitor protecting a critical system resource when it is suspended, no
     *   thread can access this resource until the target thread is resumed. If
     *   the thread that would resume the target thread attempts to lock this
     *   monitor prior to calling <code>resume</code>, deadlock results.  Such
     *   deadlocks typically manifest themselves as "frozen" processes.
     *   For more information, see
     *   <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html" rel="external nofollow" >Why
     *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
     */
    @Deprecated
    public final void suspend() {
        checkAccess();
        suspend0();
    }

    @Deprecated
    public final void resume() {
        checkAccess();
        resume0();
    }    

Object.wait

wait 會(huì)把當(dāng)前持有的鎖釋放掉同時(shí)阻塞住,讓出 CPU。當(dāng)其他線程調(diào)用 Object.notify/notifyAll 時(shí),會(huì)被喚醒,可能得到 CPU,并且獲得鎖。

LockSupport.park

這就是鎖了嘛,相對(duì)應(yīng)的用 unpark 解鎖。

Thread.stop

該方法已過(guò)時(shí),直接停止線程,同時(shí)會(huì)釋放所有鎖,太過(guò)暴力,容易導(dǎo)致數(shù)據(jù)不一致。

到此這篇關(guān)于Java如何讓線程主動(dòng)讓出 CPU的文章就介紹到這了,更多相關(guān)Java 主動(dòng)讓出 CPU內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java線程池工作隊(duì)列飽和策略代碼示例

    java線程池工作隊(duì)列飽和策略代碼示例

    這篇文章主要介紹了java線程池工作隊(duì)列飽和策略代碼示例,涉及線程池的簡(jiǎn)單介紹,工作隊(duì)列飽和策略的分析及代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java?詳細(xì)講解線程的狀態(tài)及部分常用方法

    Java?詳細(xì)講解線程的狀態(tài)及部分常用方法

    在Java程序中,一個(gè)線程對(duì)象只能調(diào)用一次start()方法啟動(dòng)新線程,并在新線程中執(zhí)行run()方法。一旦run()方法執(zhí)行完畢,線程就結(jié)束了,本篇來(lái)講解Java線程的狀態(tài)以及部分常用方法
    2022-04-04
  • Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式

    Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式

    這篇文章主要介紹了Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式,本文通過(guò)兩種方式給大家介紹創(chuàng)建對(duì)象的方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Java 超詳細(xì)講解異常的處理

    Java 超詳細(xì)講解異常的處理

    異常就是不正常,比如當(dāng)我們身體出現(xiàn)了異常我們會(huì)根據(jù)身體情況選擇喝開(kāi)水、吃藥、看病、等 異常處理方法。 java異常處理機(jī)制是我們java語(yǔ)言使用異常處理機(jī)制為程序提供了錯(cuò)誤處理的能力,程序出現(xiàn)的錯(cuò)誤,程序可以安全的退出,以保證程序正常的運(yùn)行等
    2022-04-04
  • 設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化

    設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化

    這篇文章主要介紹了設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化方法,需要的朋友可以參考下
    2014-10-10
  • Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn)

    Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn)

    這篇文章主要介紹了Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn),頁(yè)面靜態(tài)化是將原來(lái)的動(dòng)態(tài)網(wǎng)頁(yè)改為通過(guò)靜態(tài)化技術(shù)生成的靜態(tài)網(wǎng)頁(yè),FreeMarker?是一個(gè)用?Java?語(yǔ)言編寫(xiě)的模板引擎,它基于模板來(lái)生成文本輸,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-06-06
  • Java實(shí)現(xiàn)視頻格式轉(zhuǎn)化的操作代碼

    Java實(shí)現(xiàn)視頻格式轉(zhuǎn)化的操作代碼

    在當(dāng)今數(shù)字化時(shí)代,視頻已成為我們?nèi)粘I詈凸ぷ髦胁豢苫蛉钡囊徊糠?不同的設(shè)備和平臺(tái)可能支持不同的視頻格式,因此,視頻格式轉(zhuǎn)換的需求也日益增長(zhǎng),本文將介紹如何使用Java實(shí)現(xiàn)視頻格式轉(zhuǎn)換,需要的朋友可以參考下
    2025-01-01
  • Java 包和訪問(wèn)權(quán)限操作

    Java 包和訪問(wèn)權(quán)限操作

    這篇文章主要介紹了Java 包和訪問(wèn)權(quán)限操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄(思路詳解)

    springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄(思路詳解)

    這篇文章主要介紹了springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問(wèn)題及解決方法(親測(cè)可用)

    IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)

    這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問(wèn)題及解決方法,親測(cè)試過(guò)好用,需要的朋友可以參考下
    2020-04-04

最新評(píng)論

曲靖市| 南木林县| 佛学| 江口县| 左权县| 龙泉市| 南雄市| 万源市| 观塘区| 和龙市| 云阳县| 德阳市| 六盘水市| 盱眙县| 孝昌县| 广丰县| 威海市| 青岛市| 土默特左旗| 阿拉善左旗| 新闻| 商南县| 新兴县| 湖南省| 嘉义县| 宜兰县| 南岸区| 麦盖提县| 边坝县| 双鸭山市| 达拉特旗| 萨嘎县| 涟水县| 山阴县| 青海省| 成都市| 内黄县| 淄博市| 马山县| 苏尼特右旗| 桂东县|