zoukankan      html  css  js  c++  java
  • ConcurrentModificationException

    下面两个例子, 第一个不会出现Exception, 后面的会有ConcurrentModificationException。

    package com.karl.test;

    import java.util.List;
    import java.util.Vector;

    public class TestVectorConcurrent {
        Object obj = new Object();
        public static List<String> v = new Vector<String>();

        private static void init() {
            for (int i = 1; i <= 20; i++) {
                v.add("helloworld:" + i);
            }
        }

        public static void main(String[] args) {
            init();
            TestVectorConcurrent tvc = new TestVectorConcurrent();
            TestVectorConcurrent.T1 tv1 = tvc.new T1();
            Thread t1 = new Thread(tv1);
            TestVectorConcurrent.T2 tv2 = tvc.new T2();
            Thread t2 = new Thread(tv2);
            t1.start();
            t2.start();
        }

        private void retrieve() {
            synchronized (obj) {
                for (String s : v) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(s);
                }
            }
        }

        private void remove() {
            synchronized (obj) {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
                v.remove(10);
            }
        }

        class T1 implements Runnable {
            @Override
            public void run() {
                retrieve();
            }
        }

        class T2 implements Runnable {
            @Override
            public void run() {
                remove();
            }
        }
    }
    package com.karl.test;

    import java.util.List;
    import java.util.Vector;

    public class TestVectorConcurrent {

        public static List<String> v = new Vector<String>();

        private static void init() {
            for (int i = 1; i <= 20; i++) {
                v.add("helloworld:" + i);
            }
        }

        public static void main(String[] args) {
            init();
            TestVectorConcurrent tvc = new TestVectorConcurrent();
            TestVectorConcurrent.T1 tv1 = tvc.new T1();
            Thread t1 = new Thread(tv1);
            TestVectorConcurrent.T2 tv2 = tvc.new T2();
            Thread t2 = new Thread(tv2);
            t1.start();
            t2.start();
        }

        private void retrieve() {
            for (String s : v) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                System.out.println(s);
            }

        }

        private void remove() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            v.remove(10);

        }

        class T1 implements Runnable {
            @Override
            public void run() {
                retrieve();
            }
        }

        class T2 implements Runnable {
            @Override
            public void run() {
                remove();
            }
        }

  • 相关阅读:
    Java Servlet 中文API说明
    HIbernate主键详解
    ERP项目管理的五个要点
    java反射机制学习总结
    Spring 2.0的新特性和应用实践
    【Struts1.2总结】strutsconfig.xml配置
    数据库设计
    关于URL后面传中文方法总结
    JAVA开发者最常去的20个英文网站
    Socket 死连接详解
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2721547.html
Copyright © 2011-2022 走看看