源无极

导航

 

ava中LinkedList是线程不安全的,那么如果在多线程程序中有多个线程访问LinkedList的话会出现什么问题呢?

 @Test
    public void ArrayListSafeTest() throws InterruptedException {
      
       List<Integer> list = new LinkedList<>();
        // 线程A将1-999添加到列表
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程A");
                for (int i = 1; i < 1000; i++) {
                    list.add(i);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        // 线程B将1001-2000添加到列表
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程B");
                for (int i = 1000; i <= 2000; i++) {
                    list.add(i);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        Thread.sleep(1000);
        // 打印所有结果
        for (int i = 0; i < list.size(); i++) {
            System.out.println("第" + (i + 1) + "个元素为:" + list.get(i));
        }
    }

 

 

使用 多线程安全版 ConcurrentLinkedQueue

 @Test
    public void ArrayListSafeTest() throws InterruptedException {
       // List<Integer> list = new ArrayList<>();
        // List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
        //List<Integer> list = new CopyOnWriteArrayList<>();
      // List<Integer> list = new LinkedList<>();
        ConcurrentLinkedQueue<Integer> list = new ConcurrentLinkedQueue<>();
        // 线程A将1-999添加到列表
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程A");
                for (int i = 1; i < 1000; i++) {
                    list.offer(i);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        // 线程B将1001-2000添加到列表
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程B");
                for (int i = 1000; i <= 2000; i++) {
                    list.offer(i);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        Thread.sleep(1000);
        // 打印所有结果
        Iterator<Integer> iterator = list.iterator();
        System.out.println("大小: "+list.size());
       /* while (iterator.hasNext()){
            Integer next = iterator.next();
           // System.out.println("next= "+next);
        }*/

    }

 

 

 

posted on 2019-08-29 10:37  源无极  阅读(980)  评论(0编辑  收藏  举报