這是一個過期的方法,與void resume()搭配使用來暫停和喚醒一個線程。這兩個方法有可能形成獨佔和不一樣步的問題—詳見《java多線程編程核心技術》。方法源碼以下:html
@Deprecated public final void suspend() { checkAccess(); suspend0(); }
首先當前線程的checkAccess()方法被調用,其有可能致使SecurityException。若是當前線程時活躍着的,則掛起,調用resume()方法激活。相關方法checkAccess()和suspend0()源碼以下:java
/** *檢測當前運行的線程是否有權限修改這個線程 * 好比main線程使用代碼someThread.suspend()來修改someThread線程; *若是有 安全管理器/SecurityManagy,則將被修改的線程做爲參數調用安全管理器的 *checkAccess方法。 * *若是當前線程沒有權限修改被修改的線程,則拋出SecurityException異常。 */ public final void checkAccess() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkAccess(this); } }
native指Java調用本地方法,一般爲非java語言實現編程
private native void suspend0();//原來源碼名稱後綴數字的是這個意思
resume()方法同理,也是一個過期的喚醒線程的方法。若是當前線程沒有權限修改被喚醒的線程,則拋出SecurityException異常
安全
@Deprecated public final void resume() { checkAccess(); resume0(); }
void yield()
Thread.yield()
是指當前線程已經完成重要操做,建議線程調度器將cpu資源從轉移給另外一個線程:
多線程
public static native void yield();
線程優先級爲1~10,數字越高表明級別越高優先級越高。Thread類中設置了三個默認的優先級屬性:
ide
/** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;
當咱們在使用void setPriority(int newPriority) 設置線程的優先級時,數字必須在這個區間。方法源碼以下:函數
/** *final方法:不能被改寫;final類不能被繼承; * * * */ public final void setPriority(int newPriority) { ThreadGroup g; checkAccess();//檢查當前線程是否有修改此線程優先級的權限; if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {//優先級必須在[1,10]之間,不然拋異常:非法參數 throw new IllegalArgumentException(); } //fixme 返回被修改線程所在的線程組,若是被修改線程結束則返回null。若是要設置的值大於所在組線程的能夠有的最大值,則自動下降爲當前線程組已有的最大值。 if((g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); } }
這裏有一個須要注意的點:若是要設置的值大於所在組線程的能夠有的最大值,則自動下降爲當前線程組已有的最大值。除此以外:
1.線程具備繼承性,即被建立的線程與其建立者具備相同的優先級;
2.線程優先級即具備「規則性」,又具備「隨機性」,即優先級高的線程有限執行,但其run()方法中任務又不必定是最早執行完。cup只是儘可能將資源讓給優先級比較高的線程,可是代碼先運行、優先級高的線程不必定最早執行完。
綜上,線程的優先級具備1.組自貶性;2.繼承性;3.隨機性測試
守護進程也可理解爲「保姆進程」,當所須要服務的「僱主進程們」結束了,保姆進程自動銷燬。最典型的守護進程是GC垃圾回收器。調用Thread 的setDaemon(boolean )方法傳參true將進程設置爲守護進程,此方法只能在start()調用之間調用,即進程爲非存活的狀態下。方法源碼以下:this
public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) {//若是當前線程已經經過start()方法激活,則不能在更改其類型。 throw new IllegalThreadStateException(); } daemon = on;//daemon是Thread類變量,標識其是不是守護進程 }
中止一個線程使用interrupt()
方法
除非線程要中斷自己。此操做是爲當前線程作標識「能夠中止」,而非真正的中止線程。線程
若是線程在調用wait()、join()、sleep()
及其各類變參多態函數後,再調用interrupt()
方法會清空其interurpted
的狀態,並拋出InterruptedException
異常。
線程是不是「可中止狀態」能夠調用兩個方法:
boolean interrupted()
:會清除「可中止狀態」,測試的是當前線程;2.boolean isInterrupted()
:不會清除狀態,測試的是Thread對象;
//參數表示返回其當前狀態後是否將其從新設置爲「不可中止」 private native boolean isInterrupted(boolean ClearInterrupted);
兩個方法源碼以下:
public static boolean interrupted() { return currentThread().isInterrupted(true); } public boolean isInterrupted() { return isInterrupted(false); }
中止線程書中講解了三種方式:
異常法即當咱們使用interrupted()(當前current線程)已經標記爲「可中止」時,能夠在run()方法中拋出異常並在run()方法的最後捕獲異常,以此來中止線程。示例代碼以下:
public DemoClass extends Thread{ @Override public void run(){ try{ //doSomeThing if(this.interrupted()){ throw new InterruptedException(); } }catch(InterruptedException e){ //doSomeThing } } }
正如咱們上邊講的,若是線程調用wait()、join()、sleep()後再調用interrupt()方法,會清空中止狀態並拋異常InterreptedException。使用這個方法也能夠中止線程,其實與第一個差很少,我以爲。
調用sleep(long million)或者sleep(long millis,int nans)可使調用方法的線程休眠指定時間,方法可能拋出InterruptedException和IllegalArgumentException,源碼以下:
//使當前線程休眠指定毫秒:1秒=1000毫秒 public static native void sleep(long millis) throws InterruptedException; /** *使線程休眠指定毫秒+納秒,其實精確不了納秒的級別: * 1.若是納秒參數>500,000則休眠時間增長1毫秒(至關於向上取整); * 2.若是毫秒參數爲0並且納秒參數不爲0,則休眠1毫秒; * 3.納秒級別的參數不能超過1毫秒,即999,999; * 1ms=1000,000ns(納秒) * **/ public static void sleep(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) {//納秒級別的參數不能超過1毫秒 throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis++; } sleep(millis); }
方法源碼以下,若是當前線程正在運行或者準備運行都會返回true:
public final native boolean isAlive();
public static native Thread currentThread();