如上代码
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AutoInc {
public static int res = 0;
public static int get() {
res++;
return res;
}
public static void main(String[] args) throws Exception {
ExecutorService es = Executors.newFixedThreadPool(15);
for (int i = 0; i < 15; i++) {
es.submit(new Runnable() {
@Override
public void run() {
int myres = 0;
for (int j = 0; j < 1000; j++) {
myres = get();
}
System.out.println(Thread.currentThread() + " res:" + myres);
}
});
}
es.shutdown();
Thread.sleep(1000 * 5);
// while(!es.isTerminated()){
//
// }
if (res != 15000) {
System.out.println("finalCountL not matched." + res);
}
}
}