多執行緒與高併發4 執行緒之間通訊

2021-10-04 09:01:26 字數 1987 閱讀 6563

synchronied(鎖公升級)

volitile

atomic***x longadder

reentrantlock(cas)

countdownlatch

cyclicbarrier

phaser

readwritelock stampedlock

semaphore

exchanger

locksupport

static string[

] words =

;static string[

] num =

;static thread t1=null;

static thread t2=null;

public

static

void

main

(string[

] args)})

; t2 =

newthread((

)->})

; t1.

start()

; t2.

start()

;}

public

synchronized

void

put(t t)

catch

(interruptedexception e)}

lists.

add(t)

;++count;

this

.notifyall()

;//通知消費者執行緒進行消費

}public

synchronized t get()

catch

(interruptedexception e)

} t = lists.

removefirst()

; count --

;this

.notifyall()

;//通知生產者進行生產

return t;

}

瑕疵在於,notifyall會叫醒所有,不論生產者和消費者

private lock lock =

newreentrantlock()

;private condition producer = lock.

newcondition()

;private condition consumer = lock.

newcondition()

;public

void

put(t t)

lists.

add(t)

;++count;

consumer.

signalall()

;//通知消費者執行緒進行消費

}catch

(interruptedexception e)

finally

}public t get()

t = lists.

removefirst()

; count --

; producer.

signalall()

;//通知生產者進行生產

}catch

(interruptedexception e)

finally

return t;

}

1、nonfairsync->sync->aqs

2、state和共同操作該state的雙向鍊錶。

3、cas+volitile state

4、state是volitile修飾的,並且設定state方法除了有setstate()和compareandstate();

5、aqs主要方法:tryacquire,tryrelease,tryacquireshared,tryreleaseshared,isheldexclusively

併發程式設計 多執行緒之間通訊

多執行緒之間實現通訊 多執行緒之間如何實現通訊 什麼是多執行緒之間通訊?多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。畫圖演示 多執行緒之間通訊需求 需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。實現基本實現 共享...

4 多執行緒之間實現通訊

目錄 知識點1 多執行緒之間如何實現通訊 1 什麼是多執行緒之間通訊?2 多執行緒之間通訊需求 3 實現基本實現 1 共享資源源實體類 2 輸入執行緒資源 3 輸出執行緒 4 執行 5 解決執行緒安全問題 知識點2 wait notify方法 知識點3 wait與sleep區別 知識點4 lock鎖...

多執行緒之間通訊

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。共享資源源實體類 class res輸入執行緒資源 class intthrad extends thread overr...