Java如何讓線程主動(dòng)讓出 CPU
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?詳細(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ì)象的方式,本文通過(guò)兩種方式給大家介紹創(chuàng)建對(duì)象的方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
設(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),頁(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)化的操作代碼
在當(dāng)今數(shù)字化時(shí)代,視頻已成為我們?nèi)粘I詈凸ぷ髦胁豢苫蛉钡囊徊糠?不同的設(shè)備和平臺(tái)可能支持不同的視頻格式,因此,視頻格式轉(zhuǎn)換的需求也日益增長(zhǎng),本文將介紹如何使用Java實(shí)現(xiàn)視頻格式轉(zhuǎn)換,需要的朋友可以參考下2025-01-01
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)
這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問(wèn)題及解決方法,親測(cè)試過(guò)好用,需要的朋友可以參考下2020-04-04

