多執行緒基礎

2021-10-23 07:31:05 字數 3352 閱讀 4006

public

class

test

}class

mythread

extends

thread

}

public

class

test

}class

myrunnable

implements

runnable

}

public

class

test

}class

mycallable

implements

callable

return sum;

}}

一般在建立後,執行緒池裡面的執行緒池數量為0,在有任務來的時候才建立。

threadpoolexecutor poll=

newthreadpoolexecutor(3

,5,2000

, timeunit.milliseconds,

newlinkedblockingdeque

<

>(5

),newthreadpoolexecutor.abortpolicy()

);

corepollsize:核心執行緒數量

maximumpollsize:最大執行緒數量,核心執行緒+非核心執行緒

keepalivetime:非核心執行緒存活的最大空閒時間,也就是在沒有任務的情況下非核心執行緒的最大存活時間

timeunit:存活的時間單位

workqueue:阻塞佇列,用來存放任務

threadfactory:生產線程的工廠

handler:拒絕策略,有4種:

abortpolicy:直接丟棄,並丟擲異常

discardpolicy:直接丟棄,但並不會拋異常

discordoldestpolicy:丟棄隊頭任務,然後嘗試執行

callerrunspolicy:呼叫執行緒去執行

執行過程:當有任務來的時候,執行緒池檢視自己是否還有空閒的核心執行緒,如果有就讓核心執行緒去執行任務,如果沒有,就嘗試把任務放到工作佇列裡,如果能放下就放,如果放不下就看現在的執行緒數是否已經到達最大執行緒數量,如果沒有達到就建立執行緒去執行,如果已經到達了最大執行緒數就採取拒絕策略。

還可以通過exector來建立:

executorservice e1=executors.

newsinglethreadexecutor()

; executorservice e2=executors.

newcachedthreadpool()

; executorservice e3=executors.

newfixedthreadpool(4

);

//thread呼叫為靜態方法,thread呼叫為例項方法

runnable r=()

-> system.out.

println

("執行緒");

thread thread=

newthread

(r);

//每個執行緒都有唯一的id

long id=thread.

getid()

;//獲取執行緒名字

string name=thread.

getname()

;//設定執行緒名字

thread.

setname

("執行緒2");

//獲取執行緒優先順序,優先順序高的有可能會被優先執行,但不是一定會被先執行

int priority=thread.

getpriority()

;//設定執行緒優先順序

thread.

setpriority(2

);//獲取執行緒狀態,執行緒轉態是個列舉類

thread.state s=thread.

getstate()

;//new runnable blocked waiting timed_waiting terminated

for(thread.state state:thread.state.

values()

)//判斷執行緒是否是守護執行緒,jvm會在乙個程序的所有非守護執行緒都結束後才會關閉

//比如:gc就是守護執行緒

boolean isd=thread.

isdaemon()

;//判斷執行緒是否存活

boolean isa=thread.

isalive()

;//當前活躍的執行緒數量

thread.

activecount()

;//執行緒讓步

thread.

yield()

;//讓執行緒睡眠

thread.

sleep

(3000);

//等執行緒結束

thread.

join()

;/** interrupt()並不會立刻停止乙個執行緒,而是設定乙個中斷標誌位,如果當前執行緒

* 因為呼叫了wait(),join(),sleep()等而處於阻塞狀態,呼叫interrupt()就會

* 拋乙個異常,清空所有標誌位,如果當前執行緒並沒有處於阻塞狀態,就會設定一

* 個中斷標誌位。

* interrupted() 判斷是否設定中斷標誌位,如果設定了會清空中斷標誌位

* isinterrupted() 判斷是否設定中斷標誌位,如果設定了並不會清空中斷標誌位

* */

thread.

interrupt()

; thread.

interrupted()

; thread.

isinterrupted()

;/** wait(),notify(),notifyall() 只能用於同步**塊或同步方法中,用於執行緒間的通訊

* 呼叫wait(),當前執行緒釋放物件鎖,並處於阻塞狀態,阻塞在wait()**行處,等其它執行緒

* 呼叫當前物件的notify()或notifyall()才能被喚醒,也不是立刻就被喚醒,需要等呼叫

* notify()或notifyall()的執行緒釋放物件鎖之後才能被喚醒

* * notify()隨機喚醒乙個執行緒,notifyall()喚醒所有執行緒

* * */

object o=

newobject()

;synchronized

(o)

多執行緒基礎

對於多執行緒程式設計,很多人概念不清,寫 的時候要麼是處處加鎖,影響效能不說,還容易莫名其妙的死鎖,還有人對多執行緒敬而遠之。所以學習多執行緒程式設計最重要的不是學習 api,而是理解什麼才是多執行緒安全的 從例子說起 include include long global1 0 volatile ...

多執行緒基礎

什麼是併發 同時執行多個程式,或者乙個程式的多段 在巨集觀上,存在併發的,但是在微觀上,其實不存在併發 時間片 在計算機中,使用時間片來實現併發的運算 在計算甲中,在最小的單位時間上 只能執行乙個運算 用來控制多個程式之間的輪轉,使得程式交替的執行 達到併發的目的 多個cpu 多個核心 才能實現真正...

多執行緒基礎

多執行緒的最底層依賴於unsafe的compareandswap cas 和locksupport的park和unpark操作。cas需要傳遞兩個引數 expect和update。先跟第乙個引數expect進行比較,如果等於第乙個引數,那麼就將該值設定為第二個引數,這是由硬體提供的原子操作,所以不會...