zoukankan      html  css  js  c++  java
  • Java多线程系列五——列表类

    参考资料:

    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());
        }
    }
  • 相关阅读:
    SQL server 导出平面文件时出错: The code page on Destination
    中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
    C# DataTable 转 List(大家进来讨论讨论)
    CSS3圆角气泡框,评论对话框
    WinForm 换行问题 textbox (转)
    Nhibernate 多对多级联删除
    JS、C# 去除html标签
    Nhibernate 多对多级联更新
    Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
    ExtJs 4.2.1 报错:Uncaught TypeError: Cannot call method 'getItems' of null
  • 原文地址:https://www.cnblogs.com/hiver/p/7122859.html
Copyright © 2011-2022 走看看