Vector线程安全问题

发布时间:2019-11-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Vector线程安全问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文主要讲述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();                 }             }         });     } }

异常@H_659_126@
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

脚本宝典总结

以上是脚本宝典为你收集整理的Vector线程安全问题全部内容,希望文章能够帮你解决Vector线程安全问题所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。