日期:2014-05-18 浏览次数:20667 次
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;
public class Sync {
public static AtomicInteger i = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println(i);
ArrayList<FutureTask> tasks = new ArrayList<FutureTask>(1000);
for(int i = 0; i < 1000; i++){
FutureTask task = new FutureTask(new Runnable() {
@Override
public void run() {
Sync.incrementI();
}
}, null);
new Thread(task).start();
tasks.add(i,task);
}
for(int i = 0; i < 1000; i++){
tasks.get(i).get();
}
System.out.println(i);
}
public static void incrementI(){
for(int i =0 ;i<1000;i++)
Sync.i = new AtomicInteger(Sync.i.incrementAndGet());
}
}