java并发

This commit is contained in:
罗祥
2019-11-29 17:58:46 +08:00
parent 3f371f110d
commit 538f89ebc9
14 changed files with 913 additions and 114 deletions

View File

@ -31,6 +31,7 @@ public class J5_AtomicIntegerFieldUpdater {
CountDownLatch latch = new CountDownLatch(number);
ExecutorService executorService = Executors.newFixedThreadPool(10);
Candidate candidate = new Candidate("候选人", 0);
// 使用字段更新型来保证其线程安全
AtomicIntegerFieldUpdater<Candidate> fieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Candidate.class, "score");
for (int i = 0; i < number; i++) {
executorService.execute(new Task(latch, candidate, fieldUpdater));