多執行緒(併發)學習筆記

2021-06-07 08:09:03 字數 1169 閱讀 8238

1,無論以哪種方式啟動乙個執行緒,要給執行緒乙個名字,對排錯、監控有幫助。

2,要對執行緒interrupt做處理。

3,futrure 是任務的提交者和執行者之間的通訊手段,get()會阻塞;

executorservice executor = executors.newsinglethreadexecutor();

callabletask = new callable()

};futurefuture =executor.submit(task);

future.get();

4,阻塞佇列,常用的資料結構,用於生產者消費者模式,常用三種阻塞隊列為arrayblockingqueue,linkedblockingqueue,synchronousqueue,

使用阻塞佇列時不要使用從queue繼承下來的方法,否則失去blocking特性。

在blockingqueue中那個,使用put和take,而非offer和poll。

下面是用lock實現blockingq的pv操作

public class blockingq 

if(linkedlist.size() == maxlenght)

return linkedlist.poll();

}finally

}public void offer(object object) throws interruptedexception

if(linkedlist.size() == maxlenght)

linkedlist.add(object);

}finally

}}

5,reentrantlock和synchronized的關係

synchronized是lock的一種簡化實現,乙個lock可以對應多個condition,而syschronized把lock和condition合併了,可以說syschronized是lock簡易版本,而且效率沒有lock高。

6,代替鎖得辦法,硬體原子操作,lockfree演算法。lockfree演算法分三個步驟,迴圈,cas(compare and set),返回。

7,concurrenthashmap資料結構使用了lockfree的演算法,支援多個writer併發。

未完待續。。。。

c 多執行緒併發學習筆記 2

等待乙個時間或其他條件 在乙個執行緒等待完成任務時,會有很多選擇 1.它可以持續的檢查共享資料標誌 用於做保護工作的互斥量 直到另乙個執行緒完成工作時對這個標誌進行重設。缺點 資源浪費,開銷大 2.在等待執行緒的檢查間隙,使用std this thread sleep for 進行週期性的間歇。缺點...

多執行緒高併發程式設計學習筆記三

高併發程式設計學習筆記三 併發容器 佇列 1.map set的選擇使用 1.不需要執行緒安全 hashmap 無序的key value 1.建立table來儲存entry 2.hashcode key 得到鍵值對應該存放的位置 3.用equal方法比較key,如果相同覆蓋資料,如果不同接到下面形成鍊...

多執行緒併發

多執行緒併發主要有3個方面 1 同步器 主要有synchronized,reentrantlock 訊號量,門栓 countdownlatch 障柵 cyclicbarrier 交換器。2 同步容器 主要包括 對映 集 佇列 對映 concurrenthashmap,concurrentskipli...