多線程之類(lèi)

  1. AtomicInteger類(lèi):可以對(duì)基本數(shù)據(jù)/數(shù)組中的基本數(shù)據(jù)/類(lèi)中的基本數(shù)據(jù)進(jìn)行操作
  2. Executors類(lèi):
    a. ExecutorService threadPool = Executors.newFixedThreadPool(3);固定大小線程池
    b. ExecutorService threadPool = Executors.newCachedThreadPool();緩存線程池
    c. ExecutorService threadPool = Executors.newSingleThreadExecutor();單一線程池
  3. Callable&Future:
    a. Callable采用ExecutorService的submit方法提交,返回的future對(duì)象可以取消任務(wù)
    b. Future取得的結(jié)果類(lèi)型和Callable返回的結(jié)果類(lèi)型必須一致,通過(guò)泛型來(lái)實(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;
                }
            });
        }
  1. Lock&Condition
    a. 兩個(gè)線程要實(shí)現(xiàn)互斥,它們必須用同一個(gè)Lock對(duì)象。
    b. Lock與synchronize作用一樣,鎖本身是一個(gè)對(duì)象
    c. 讀寫(xiě)鎖【多個(gè)讀鎖不互斥、讀鎖寫(xiě)鎖互斥、寫(xiě)鎖寫(xiě)鎖互斥】
    d. Condition功能類(lèi)似Object的wait和notify方法
    e. 一個(gè)鎖可以有多個(gè)Condition,即有多路等待和通知

  2. Semaphore信號(hào)燈
    a. 可以維護(hù)當(dāng)前訪問(wèn)自身的線程個(gè)數(shù),并提供同步機(jī)制
    b. 單個(gè)semaphore可以實(shí)現(xiàn)互斥鎖功能【一個(gè)線程獲得鎖,再由另一個(gè)線程釋放鎖】

  3. 隊(duì)列BlockingQueue
    a. 只有put方法和take方法才具有阻塞效果
    b. 用兩個(gè)具有1個(gè)空間的隊(duì)列來(lái)實(shí)現(xiàn)同步通知功能
    c. 與Semaphore相似;但隊(duì)列是一方存放數(shù)據(jù),一方釋放數(shù)據(jù);而Semaphore通常是由同一方設(shè)置和釋放信號(hào)量
    d.ArrayBlockingQueue & LinkedBlockingDeque

  1. 同步集合【java5提供的】
    a. ConcurrentHashMap
    b. ConcurrentSkipListMap
    c. ConcurrentSkipListSet
    d. CopyOnWriteArrayList
    e. CopyOnWriteArraySet

  2. 使用了兩個(gè)condition?。?!

condition.jpg
condition2.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容