1

本文主要讲述java的vector的线程安全问题

实例

vector是线程安全的,只是说它的多线程发生单次读写不会出现问题,并不代表两次读/写操作过程中没有任何其他的线程写入数据。

public class VectorTest {
    private Vector vector = new Vector();
    public void add(int data1,int data2) throws InterruptedException {
        vector.add(data1);
        Thread.sleep(1000);
        vector.add(data2);
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName()+"-get[0]:"+vector.get(0));
        System.out.println(Thread.currentThread().getName()+"-get[1]:"+vector.get(1));
        vector.clear();
    }
    public static void main(String[] args) throws InterruptedException {
        final VectorTest demo = new VectorTest();
        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    demo.add(1,2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    demo.add(3,4);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

异常

pool-1-thread-1-get[0]:1
pool-1-thread-1-get[1]:3
Exception in thread "pool-1-thread-2" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.util.Vector.get(Vector.java:744)
    at com.persia.senior.chap5.security.VectorTest.add(VectorTest.java:19)
    at com.persia.senior.chap5.security.VectorTest$2.run(VectorTest.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

原义的线程安全

是指:对于ArrayList,LinkedList,HashMap,HashSet,StringBuilder等,如果没有写操作,都线程安全的,只有读写同时发生时才出现线程不安全。

实例

public class VectorTest {
    private Vector vector = new Vector();
    private List list = new ArrayList();
    public void addVector(int data) throws InterruptedException {
        vector.add(data);
    }
    public void addList(int data) throws InterruptedException {
        list.add(data);
    }
    public void printInfo(){
        System.out.println("vector:"+vector.size());
        System.out.println("list:"+list.size());
    }
    public static void main(String[] args) throws InterruptedException {
        final VectorTest demo = new VectorTest();
        ExecutorService pool = Executors.newCachedThreadPool();
        for(int i=0;i<10000;i++){
           final int idx = i;
           pool.execute(new Runnable() {
               @Override
               public void run() {
                   try {
                       demo.addVector(idx);
                       demo.addList(idx);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           });
        }
        Thread.sleep(5000);
        demo.printInfo();
    }
}

输出

vector:10000
list:9501

codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论