package test;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyTest {
public static int count=0;
public synchronized static int plus(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return MyTest.count++;
}
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
ArrayList list = new ArrayList();
for(int i=0;i<1000;i++){
list.add(Executors.callable(new Runnable(){
@Override
public void run() {
// System.out.println(MyTest.plus());
MyTest.plus();
}
}));
};
try {
service.invokeAll(list);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("total:"+count);
}
}
多线程问题
最新推荐文章于 2023-11-12 11:35:54 发布