- AtomicInteger類:可以對(duì)基本數(shù)據(jù)/數(shù)組中的基本數(shù)據(jù)/類中的基本數(shù)據(jù)進(jìn)行操作
- Executors類:
a. ExecutorService threadPool = Executors.newFixedThreadPool(3);固定大小線程池
b. ExecutorService threadPool = Executors.newCachedThreadPool();緩存線程池
c. ExecutorService threadPool = Executors.newSingleThreadExecutor();單一線程池 - Callable&Future:
a. Callable采用ExecutorService的submit方法提交涧窒,返回的future對(duì)象可以取消任務(wù)
b. Future取得的結(jié)果類型和Callable返回的結(jié)果類型必須一致,通過泛型來實(shí)現(xiàn)
Future<String> future = threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(2000);
return "hello";
}
});
CompletionService<Integer> completionService = new ExecutorCompletionService<~>(threadPool2);
for (int i = 0; i < 10; i++) {
final int seq = i;
completionService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
Thread.sleep(new Random().nextInt(5000));
return seq;
}
});
}
Lock&Condition
a. 兩個(gè)線程要實(shí)現(xiàn)互斥驹止,它們必須用同一個(gè)Lock對(duì)象。
b. Lock與synchronize作用一樣,鎖本身是一個(gè)對(duì)象
c. 讀寫鎖【多個(gè)讀鎖不互斥截珍、讀鎖寫鎖互斥蜓谋、寫鎖寫鎖互斥】
d. Condition功能類似Object的wait和notify方法
e. 一個(gè)鎖可以有多個(gè)Condition亏狰,即有多路等待和通知Semaphore信號(hào)燈
a. 可以維護(hù)當(dāng)前訪問自身的線程個(gè)數(shù)桦沉,并提供同步機(jī)制
b. 單個(gè)semaphore可以實(shí)現(xiàn)互斥鎖功能【一個(gè)線程獲得鎖每瞒,再由另一個(gè)線程釋放鎖】隊(duì)列BlockingQueue
a. 只有put方法和take方法才具有阻塞效果
b. 用兩個(gè)具有1個(gè)空間的隊(duì)列來實(shí)現(xiàn)同步通知功能
c. 與Semaphore相似金闽;但隊(duì)列是一方存放數(shù)據(jù)纯露,一方釋放數(shù)據(jù);而Semaphore通常是由同一方設(shè)置和釋放信號(hào)量
d.ArrayBlockingQueue & LinkedBlockingDeque
同步集合【java5提供的】
a. ConcurrentHashMap
b. ConcurrentSkipListMap
c. ConcurrentSkipListSet
d. CopyOnWriteArrayList
e. CopyOnWriteArraySet使用了兩個(gè)condition4摺2和省!