日期:2014-05-20 浏览次数:20665 次
import java.util.ArrayList;
import java.util.List;
public class TestThread {
public static void main(String[] args) {
List list = new ArrayList();
list.add(10000);
for(int i = 0; i < 10; i++) {
new Thread(new SubThread(list)).start();
}
System.out.println(list.get(0));
}
}
class SubThread implements Runnable {
private List list;
public SubThread(List list) {
this.list = list;
}
public void run() {
for(int i = 0; i < 1000; i++) {
synchronized (list) {
this.list.set(0, (int)this.list.get(0)-1 );
}
}
}
}