以下那个 null 为何有时 出来 有时候不出来
import java.util.ArrayList;
import java.util.Vector;
/**
* Copyright(c), 2011-2100
* <p>Cwc System
* @description:Synchronized of Vector and ArrayList
* @file name: com.cwc.util.SynchronizedCollection.java
* @create: by author axis on Mar 2, 2011 5:53:52 PM
* @since: JDK1.6u21
*/
public class SynchronizedCollection {
@SuppressWarnings("unchecked")
static ArrayList arrayList = new ArrayList(20);
@SuppressWarnings("unchecked")
static Vector vector = new Vector();
public static void main(String[] args) {
Thread threadOne = new Thread(){
@SuppressWarnings("unchecked")
public void run(){
for (int i = 0; i < 10; i++) {
arrayList.add(arrayList.size(),new Integer(i));
vector.add(vector.size(),new Integer(i));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread threadTwo= new Thread(){
@SuppressWarnings("unchecked")
public void run(){
for (int i = 0; i < 10; i++) {
arrayList.add(arrayList.size(),new Integer(i));
vector.add(vector.size(),new Integer(i));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
try {
threadOne.start();
threadTwo.start();
threadOne.join();
threadTwo.join();
System.out.println(arrayList);
System.out.println(vector);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
输出结每次不一样
|