/**
- 测试多线程并发获取唯一子增长的值
-
@author Administrator
*
*/
public class BB {
private int increment = 0;final static Set set = new HashSet();
final static List list = new ArrayList();
public synchronized int getauto() {
return increment++;
}
public static void main(String[] args) throws Exception {
final BB b = new BB();
Vector workers = new Vector();
for (int i = 0; i < 2; i++) {
final Thread t = new Thread(new Runnable() {@Override public void run() { synchronized (this) { for (int j = 0; j < 10000; j++) { int num = b.getauto(); System.out.println(String.valueOf(num)); set.add(num); list.add(num); } } } }); workers.add(t); t.setName("thread-" + i); t.start(); } for (Thread t : workers) { t.join(); } System.out.println("set size :" + set.size()); System.out.println("list size :" + list.size());
}
}
list和set的size不同