package com.xiancheng;
import java.util.ArrayList;
import java.util.Vector;
public class synchronizedCollectionTest {
static ArrayList al = new ArrayList(20);
static Vector vt = new Vector();
public static void main(String[] args) throws Exception {
Thread thread1 = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
al.add(al.size(),new Integer(i));
vt.add(vt.size(),new Integer(i));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread2 = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
al.add(al.size(),new Integer(i));
vt.add(vt.size(),new Integer(i));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println(al);
System.out.println(vt);
}
}
输出结果:
[0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9]
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]