多執行緒基礎

2021-10-25 14:01:43 字數 4305 閱讀 6646

執行緒是作業系統能夠進行運算排程的最小單位。它被包含在程序之中,是程序中的實際運作單位。一條執行緒指的是程序中乙個單一順序的控制流,乙個程序中可以併發多個執行緒,每條執行緒並行執行不同的任務。

簡單來說,乙個軟體至少有乙個程序,乙個程序至少包含乙個執行緒,執行緒是程式執行的最小單位

開啟任務管理器==>效能,可以看到當前電腦上程序有203個,執行緒有2741個,說明乙個程序是包含多個執行緒的,程式執行就是呼叫這些執行緒

繼承thread類,重寫run()方法

public

class

testthread1

extends

thread

}public

static

void

main

(string[

] args)

}

實現runnable介面,重寫run()方法

public

class

testrunnable1

implements

runnable

}public

static

void

main

(string[

] args)

}

檢視原始碼可以看到class thread implements runnable,注釋說明 在大多數情況下,如果僅打算重寫run()方法而沒有其他thread方法,則應使用runnable介面。 這很重要,因為類不應被子類化,除非程式設計師打算修改或增強類的基本行為。

實現callable介面,重寫call()方法

/**

* 實現callable介面的執行緒是有返回值的

* callable《返回值》

*/public

class

testcallable1

implements

callable

return

true;}

public

static

void

main

(string[

] args)

throws exception

}

新生狀態(new)執行緒被建立未呼叫其任何方法

就緒狀態(runnable)

執行緒呼叫start()方法,此時執行緒進入就緒狀態,等待cpu進行排程

執行狀態(running)

執行緒被cpu排程,執行run()方法

阻塞狀態(blocked)

執行緒已經經過執行狀態,run()方法未完成,暫時讓出cpu,讓其他就緒的執行緒獲取cpu

死亡狀態(dead)

執行緒執行完run()方法正常退出或者出現異常使執行緒終止

獲取當前執行緒名稱

thread.

currentthread()

.getname

()

使當前執行緒休眠指定時長

//使當前執行緒休眠1秒

thread.

sleep

(1000

);

獲取當前執行緒狀態

thread thread =

newthread()

;thread.state state = thread.

getstate()

;system.out.

println

("執行緒狀態"

+state)

;

合併執行緒

thread thread =

newthread()

;//join合併執行緒,此執行緒執行完成後,再執行其他執行緒,其他執行緒為阻塞狀態

thread.

join()

;

設定執行緒優先順序

thread thread =

newthread()

;//優先順序1~10 數值越大優先順序越高 優先順序高的被cpu排程的機率就越大 但是並不是優先順序高就一定先被呼叫,

thread.

setpriority(1

);thread.

start()

;

執行緒禮讓

//讓正在執行的執行緒暫停,但不阻塞,讓執行緒衝執行狀態變為就緒狀態,讓cpu重現排程,但禮讓不一定成功

thread.

yield()

;

thread thread =

newthread()

;//建立出來的執行緒預設為使用者執行緒,設定執行緒為守護執行緒,預設為false,當main()方法執行完畢,不管守護執行緒是否執行完畢都結束執行

thread.

setdaemon

(true);

thread.

start()

;

多個執行緒操作乙個物件時,如果物件沒加鎖,可能出現預期之外的資料

public

static

void

main

(string[

] args)

throws interruptedexception ).

start()

;}thread.

sleep

(1000);

system.out.

println

(list.

size());}

輸出結果

39984

由於arraylist是執行緒不安全的集合,多執行緒新增資料時可能會出現資料覆蓋的情況,此時就要對多執行緒操作的物件進行加鎖

synchronized(操作物件)

public

static

void

main

(string[

] args)

throws interruptedexception })

.start()

;}thread.

sleep

(1000);

system.out.

println

(list.

size());}

輸出結果

40000

lock.lock();

業務**

lock.unlock();

public

static

void

main

(string[

] args)

throws interruptedexception

list.

add(thread.

currentthread()

.getname()

);//釋放鎖

lock.

unlock()

;}).

start()

;}thread.

sleep

(1000);

system.out.

println

(list.

size());}

輸出結果

40000

使用lock同樣可以達到執行緒安全的目的

個人拙見,如有錯誤望指出

多執行緒基礎

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

多執行緒基礎

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

多執行緒基礎

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