Replace CyclicBarrier with CountDownLatch

This commit is contained in:
Jason Tedor 2015-10-01 01:16:33 +02:00
parent 716be91345
commit 4efe7b9c18
1 changed files with 5 additions and 10 deletions

View File

@ -23,8 +23,7 @@ import org.elasticsearch.test.ESTestCase;
import org.junit.Before; import org.junit.Before;
import java.util.*; import java.util.*;
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
@ -463,7 +462,7 @@ public class CacheTests extends ESTestCase {
// test that the cache is not corrupted under lots of concurrent modifications, even hitting the same key // test that the cache is not corrupted under lots of concurrent modifications, even hitting the same key
// here be dragons: this test did catch one subtle bug during development; do not remove lightly // here be dragons: this test did catch one subtle bug during development; do not remove lightly
public void testTorture() throws InterruptedException, BrokenBarrierException { public void testTorture() throws InterruptedException {
int numberOfThreads = randomIntBetween(2, 200); int numberOfThreads = randomIntBetween(2, 200);
final Cache<Integer, String> cache = final Cache<Integer, String> cache =
CacheBuilder.<Integer, String>builder() CacheBuilder.<Integer, String>builder()
@ -471,15 +470,11 @@ public class CacheTests extends ESTestCase {
.weigher((k, v) -> 2) .weigher((k, v) -> 2)
.build(); .build();
CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads); CountDownLatch latch = new CountDownLatch(1 + numberOfThreads);
List<Thread> threads = new ArrayList<>(); List<Thread> threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) { for (int i = 0; i < numberOfThreads; i++) {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
try { latch.countDown();
barrier.await();
} catch (InterruptedException | BrokenBarrierException e){
throw new RuntimeException(e);
}
Random random = new Random(); Random random = new Random();
for (int j = 0; j < numberOfEntries; j++) { for (int j = 0; j < numberOfEntries; j++) {
Integer key = random.nextInt(numberOfEntries); Integer key = random.nextInt(numberOfEntries);
@ -489,7 +484,7 @@ public class CacheTests extends ESTestCase {
threads.add(thread); threads.add(thread);
thread.start(); thread.start();
} }
barrier.await(); latch.countDown();
for (Thread thread : threads) { for (Thread thread : threads) {
thread.join(); thread.join();
} }