public class Counter {
public static int threadCount=1000;
public static int count=0;
public static CountDownLatch start = new CountDownLatch(threadCount);
public static CountDownLatch end =new CountDownLatch(threadCount);
public static void inc(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
public static void main(String[] args){
for(int i=0;i<1000;i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
start.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Counter.inc();
end.countDown();
}
}).start();
start.countDown();
}
try {
end.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Counter.count);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货