Java多線程系列五——列表類

來源:互聯網
上載者:User

標籤:com   star   get   err   script   enter   說明   catch   安全執行緒   

參考資料:

http://xxgblog.com/2016/04/02/traverse-list-thread-safe/

一些列表類及其特性

 類 安全執行緒 Iterator 特性 說明
Vector 是 fail-fast 內部方法用synchronized修飾,因此執行效率較低

1. 安全執行緒的列表類並不意味著調用它的代碼就一定安全執行緒

2. 只有CopyOnWriteArrayList能支援在遍曆時修改列表元素

ArrayList 否 fail-fast 在多線程環境中使用不當易出錯
Collections.synchronizedList 是 fail-fast Collections.synchronizedList可以得到安全執行緒的列表,與Vector類似
CopyOnWriteArrayList 是 fail-safe 每個線程先複製一份並把地址指向新list,在新的list上操作,因此最終結果未必符合預期,適用於經常需要遍曆但很少修改的情境

 以下代碼類比多線程環境下,各個類Iterator機制的表現

import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Vector;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.CountDownLatch;/** * http://xxgblog.com/2016/04/02/traverse-list-thread-safe/ *  * @Description: 多線程巨集指令清單 */public class ThreadListTest {    public void multipleThreadArrayList(final List<Integer> list) throws InterruptedException {        int count = 10;        for (int i = 0; i < count; i++) {            list.add(i);        }        CountDownLatch countDownLatch = new CountDownLatch(2);        new Thread(new Runnable() {            @Override            public void run() {                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                for (Integer integer : list) {                    System.out.println(integer);                }                countDownLatch.countDown();            }        }).start();        new Thread(new Runnable() {            @Override            public void run() {                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getId() + " remove " + list.remove(0));                countDownLatch.countDown();            }        }).start();        countDownLatch.await();    }    public static void main(String[] args) throws InterruptedException {        ThreadListTest test = new ThreadListTest();        List<Integer> list;        list = new Vector<>();// Vector只保證內部方法安全執行緒        list = new ArrayList<>();// ArrayList效率較Vector,但非安全執行緒        list = Collections.synchronizedList(new ArrayList<>());// Collections.synchronizedList與Vector有異曲同工之妙        list = new CopyOnWriteArrayList<>();// CopyOnWriteArrayList安全執行緒:每個線程先複製一份並把地址指向新list,在新的list上操作,因此最終結果未必符合預期        test.multipleThreadArrayList(list);        System.out.println(list.toString());    }}

 

Java多線程系列五——列表類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.