JDK 線程池ExecutorsnewCachedThreadPool() //帶緩存的 不夠時自動添加ExecutorsnewSingleThreadExecutor() //單個線程池 線程死掉後自動創建ExecutorsnewFixedThreadPool() //創建容納N個線程的ExecutorsnewScheduledThreadPool() //創建定時器線程池
executo() 無返回結果submit() 有返回結果的線程
Callable //可返還結果的線程
ExecutorService threadpol = ExecutorsnewSingleThreadExecutor()Future<String> future = threadpolsubmit(new Callable<String>(){ public String call(){ return aaaa} systemoutprint(Futureget()))
Lock lock = new ReentrantLock() //lock 對象Condition condition = locknewCondition() // 條件 通訊對象conditionawait() //不是 object的 wait conditionsignal() //喚醒
try{ locklock()……
}finally{ lockunLock()}
ReentrantReadWriteLock rwl = ReentrantReadWriteLock() //文件鎖rwlreadLock()lock()rwlwriteLock()lock()
條件等待盡量使用while(){
}
多線程關鍵字ExecutorsnewCachedThreadPool() //帶緩存的 不夠時自動添加ExecutorsnewSingleThreadExecutor() //單個線程池 死掉後再創建ExecutorsnewFixedThreadPool() //創建容納N個線程的ExecutorsnewScheduledThreadPool() //創建定時器線程池Lock lock = new ReentrantLock() //lock 互斥鎖 對象Condition condition = locknewCondition() // 條件 通訊對象Condition //條件鎖Semaphore //信號量 類似執行授權 (最多有個人可以走)
CyclicBarrier //類似集合點 (必須個人同時到才能走)
CountDownLatch //計數器 計時器效果 某時間點同時執行 CountDownLath a = new CountDownLath() aawait() untDown()Exchanger //數據交換 Exchanger a = new Exchanger()//放主線程 aexchange(asd)
ArrayBlockingQueue //阻塞隊列 ArrayBlockingQueue 啊 = new ArrayBlockingQueue() aput()//阻塞 aadd() //atack() 獲取
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26591.html