This commit is contained in:
Andrew Morgan 2017-01-18 22:50:56 +00:00
parent 66449db339
commit c60c870506
3 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.concurrent.countdownlatch;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class BrokenWorker implements Runnable {
private final List<String> outputScraper;
private final CountDownLatch countDownLatch;
public BrokenWorker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
this.outputScraper = outputScraper;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
if (true) {
throw new RuntimeException("Oh dear");
}
countDownLatch.countDown();
outputScraper.add("Counted down");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.concurrent.countdownlatch;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class Worker implements Runnable {
private final List<String> outputScraper;
private final CountDownLatch countDownLatch;
public Worker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
this.outputScraper = outputScraper;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
// Do some work
System.out.println("Doing some logic");
countDownLatch.countDown();
outputScraper.add("Counted down");
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.concurrent.countdownlatch;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
public class CountdownLatchExampleTest {
@Test
public void shouldBlockUntilLatchIsReleased() throws InterruptedException {
// Given
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
CountDownLatch countDownLatch = new CountDownLatch(5);
List<Thread> workers = Stream
.generate(() -> new Thread(new Worker(outputScraper, countDownLatch)))
.limit(5)
.collect(toList());
// When
workers.forEach(Thread::start);
countDownLatch.await(); // Block until workers finish
outputScraper.add("Latch released");
// Then
outputScraper.forEach(Object::toString);
assertThat(outputScraper)
.containsExactly(
"Counted down",
"Counted down",
"Counted down",
"Counted down",
"Counted down",
"Latch released"
);
}
@Test
public void shouldEventuallyTimeout() throws InterruptedException {
// Given
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
CountDownLatch countDownLatch = new CountDownLatch(5);
List<Thread> workers = Stream
.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch)))
.limit(5)
.collect(toList());
// When
workers.forEach(Thread::start);
final boolean result = countDownLatch.await(3L, TimeUnit.SECONDS);
// Then
assertThat(result).isTrue();
}
}