public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
final int count = i;
// int count = i;
executorService.execute(() -> {
try {
semaphore.acquire();
update(count);
semaphore.release();
} catch (Exception e) {
log.error(“exception”, e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
log.info(“size:{}”, list.size());
}
问题1:
final int count = i;
将 i 传参到线程任务中的时机是什么?
问题2:
我改用线程安全的类测,有没有 final 似乎没影响啊?
// private static List list = new ArrayList<>();
private static List list = Collections.synchronizedList(new ArrayList());