ArrayList,Vector线程安全性测试

结论:如果集合不是线程安全的话,在多线程情况下插入数据会出现数据丢失的问题。
 
Java代码  收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. //实现Runnable接口的线程  
  5. public class HelloThread implements Runnable {  
  6.     String name;  
  7.     List<String> v;  
  8.   
  9.     HelloThread(String name, List<String> v) {  
  10.         this.name = name;  
  11.         this.v = v;  
  12.     }  
  13.   
  14.     public void run() {  
  15.         System.out.println(name + "start");  
  16.         while(true) {  
  17.             v.add(name + ".add");  
  18.             System.out.println(name + " list size is " + v.size());  
  19.   
  20.             try {  
  21.                 Thread.sleep(10);  
  22.             } catch(InterruptedException e) {  
  23.                 System.out.println(e.getMessage());  
  24.             }  
  25.         }  
  26.     }  
  27.   
  28.     public static void main(String args[]) throws InterruptedException {  
  29.   
  30.         List<String> v = new ArrayList<String>();  
  31.   
  32.         HelloThread hello1 = new HelloThread("hello1", v);  
  33.         HelloThread hello2 = new HelloThread("hello2", v);  
  34.         HelloThread hello3 = new HelloThread("hello3", v);  
  35.   
  36.         Thread h1 = new Thread(hello1);  
  37.         Thread h2 = new Thread(hello2);  
  38.         Thread h3 = new Thread(hello3);  
  39.         h1.start();  
  40.         h2.start();  
  41.         h3.start();  
  42.   
  43.     }  
  44. }  

 结果:

hello3start
hello3 list size is 1
hello1start
hello1 list size is 2
hello2start
hello2 list size is 3
hello3 list size is 4 
hello1 list size is 5
hello2 list size is 4 
hello3 list size is 6
hello1 list size is 8
hello2 list size is 7
hello1 list size is 9 
hello3 list size is 10
hello2 list size is 9

 

加了12次,但size却只有10,不安全

 

改成号称线程安全的Vector:

Java代码  收藏代码
  1. import java.util.Vector;  
  2.   
  3. //实现Runnable接口的线程  
  4. public class HelloThread implements Runnable {  
  5.     String name;  
  6.     Vector<String> v;  
  7.   
  8.     HelloThread(String name, Vector<String> v) {  
  9.         this.name = name;  
  10.         this.v = v;  
  11.     }  
  12.   
  13.     public void run() {  
  14.         System.out.println(name + "start");  
  15.         while(true) {  
  16.             v.add(name + ".add");  
  17.             System.out.println(name + " vector size is " + v.size());  
  18.   
  19.             try {  
  20.                 Thread.sleep(10);  
  21.             } catch(InterruptedException e) {  
  22.                 System.out.println(e.getMessage());  
  23.             }  
  24.         }  
  25.     }  
  26.   
  27.     public static void main(String args[]) throws InterruptedException {  
  28.   
  29.         Vector<String> v = new Vector<String>();  
  30.   
  31.         HelloThread hello1 = new HelloThread("hello1", v);  
  32.         HelloThread hello2 = new HelloThread("hello2", v);  
  33.         HelloThread hello3 = new HelloThread("hello3", v);  
  34.   
  35.         Thread h1 = new Thread(hello1);  
  36.         Thread h2 = new Thread(hello2);  
  37.         Thread h3 = new Thread(hello3);  
  38.         h1.start();  
  39.         h2.start();  
  40.         h3.start();  
  41.     }  
  42. }  

结果:

hello1start
hello1 vector size is 1
hello2start
hello2 vector size is 2
hello3start
hello3 vector size is 3
hello1 vector size is 4
hello2 vector size is 5
hello3 vector size is 6
hello1 vector size is 7
hello2 vector size is 8
hello3 vector size is 9
hello1 vector size is 10
hello2 vector size is 11
hello3 vector size is 12
hello1 vector size is 13
hello3 vector size is 15
hello2 vector size is 15 
hello1 vector size is 16

 

也出现了线程不安全现象吗?不是的

 

这个不算线程不安全,加了16次,size是16,恰恰是线程安全的表现,只不过是待两个线程都add完了之后才调的size(),所以都是15,跳过了14。

 

以上一样的程序多试几次就出现了,另外关于Vector的thread-safety

All Vector methods are synchronized themselves, so as long as you are only synchronizing around a single method, your own synchronization is not necessary. If you have several method calls, which depend on each other, e.g. something like vec.get(vec.size()-2) to get the second last element, you have to use your own synchronization since otherwise, the vector may change between vec.size() and vec.get().

 

所有的Vector的方法对它们自己而言都是 synchronized的,所以如果只要同步单个方法,自己额外添加的同步措施就失去必要了。如果有几个方法需要调用,且它们互相存在依赖,比如 vec.get(vec.size()-2),是要得到倒数第二个元素,那么就必须加上自己的同步措施,因为否则的话,vector有可能在 vec.size() 和 vec.get() 之间发生改变

posted on 2014-12-24 00:38  上校  阅读(3545)  评论(0编辑  收藏  举报