Add core-java-concurrency to main pom (#2507)
* Add core-java-concurrency * Refactor codebase
This commit is contained in:
committed by
GitHub
parent
0b85b0a466
commit
515e4caaca
@@ -4,7 +4,11 @@ import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
@@ -18,7 +18,9 @@ public class CountdownLatchExampleIntegrationTest {
|
||||
// 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());
|
||||
List<Thread> workers = Stream.generate(() -> new Thread(new Worker(outputScraper, countDownLatch)))
|
||||
.limit(5)
|
||||
.collect(toList());
|
||||
|
||||
// When
|
||||
workers.forEach(Thread::start);
|
||||
@@ -26,7 +28,6 @@ public class CountdownLatchExampleIntegrationTest {
|
||||
outputScraper.add("Latch released");
|
||||
|
||||
// Then
|
||||
outputScraper.forEach(Object::toString);
|
||||
assertThat(outputScraper).containsExactly("Counted down", "Counted down", "Counted down", "Counted down", "Counted down", "Latch released");
|
||||
}
|
||||
|
||||
@@ -35,7 +36,9 @@ public class CountdownLatchExampleIntegrationTest {
|
||||
// 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());
|
||||
List<Thread> workers = Stream.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch)))
|
||||
.limit(5)
|
||||
.collect(toList());
|
||||
|
||||
// When
|
||||
workers.forEach(Thread::start);
|
||||
@@ -63,7 +66,6 @@ public class CountdownLatchExampleIntegrationTest {
|
||||
outputScraper.add("Workers complete");
|
||||
|
||||
// Then
|
||||
outputScraper.forEach(Object::toString);
|
||||
assertThat(outputScraper).containsExactly("Workers ready", "Counted down", "Counted down", "Counted down", "Counted down", "Counted down", "Workers complete");
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ public class SquareCalculatorIntegrationTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SquareCalculatorIntegrationTest.class);
|
||||
|
||||
|
||||
@Rule
|
||||
public TestName name = new TestName();
|
||||
|
||||
|
||||
@@ -49,9 +49,7 @@ public class SynchronizedHashMapWithRWLockManualTest {
|
||||
|
||||
private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
|
||||
for (int i = 0; i < threadCount; i++)
|
||||
service.execute(() -> {
|
||||
object.get("key" + threadCount);
|
||||
});
|
||||
service.execute(() -> object.get("key" + threadCount));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import static org.junit.Assert.assertNull;
|
||||
|
||||
public class ConcurrentMapNullKeyValueManualTest {
|
||||
|
||||
ConcurrentMap<String, Object> concurrentMap;
|
||||
private ConcurrentMap<String, Object> concurrentMap;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ConcurrentNavigableMapManualTest {
|
||||
public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException {
|
||||
NavigableMap<Integer, String> skipListMap = new ConcurrentSkipListMap<>();
|
||||
|
||||
updateMapConcurrently(skipListMap, 4);
|
||||
updateMapConcurrently(skipListMap);
|
||||
|
||||
Iterator<Integer> skipListIter = skipListMap.keySet().iterator();
|
||||
int previous = skipListIter.next();
|
||||
@@ -28,9 +28,9 @@ public class ConcurrentNavigableMapManualTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateMapConcurrently(NavigableMap<Integer, String> navigableMap, int concurrencyLevel) throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel);
|
||||
for (int i = 0; i < concurrencyLevel; i++) {
|
||||
private void updateMapConcurrently(NavigableMap<Integer, String> navigableMap) throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
executorService.execute(() -> {
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
for (int j = 0; j < 10000; j++) {
|
||||
@@ -45,26 +45,26 @@ public class ConcurrentNavigableMapManualTest {
|
||||
@Test
|
||||
public void givenSkipListMap_whenNavConcurrently_thenCountCorrect() throws InterruptedException {
|
||||
NavigableMap<Integer, Integer> skipListMap = new ConcurrentSkipListMap<>();
|
||||
int count = countMapElementByPollingFirstEntry(skipListMap, 10000, 4);
|
||||
int count = countMapElementByPollingFirstEntry(skipListMap);
|
||||
assertEquals(10000 * 4, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTreeMap_whenNavConcurrently_thenCountError() throws InterruptedException {
|
||||
NavigableMap<Integer, Integer> treeMap = new TreeMap<>();
|
||||
int count = countMapElementByPollingFirstEntry(treeMap, 10000, 4);
|
||||
int count = countMapElementByPollingFirstEntry(treeMap);
|
||||
assertNotEquals(10000 * 4, count);
|
||||
}
|
||||
|
||||
private int countMapElementByPollingFirstEntry(NavigableMap<Integer, Integer> navigableMap, int elementCount, int concurrencyLevel) throws InterruptedException {
|
||||
for (int i = 0; i < elementCount * concurrencyLevel; i++) {
|
||||
private int countMapElementByPollingFirstEntry(NavigableMap<Integer, Integer> navigableMap) throws InterruptedException {
|
||||
for (int i = 0; i < 10000 * 4; i++) {
|
||||
navigableMap.put(i, i);
|
||||
}
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel);
|
||||
for (int j = 0; j < concurrencyLevel; j++) {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
for (int j = 0; j < 4; j++) {
|
||||
executorService.execute(() -> {
|
||||
for (int i = 0; i < elementCount; i++) {
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
if (navigableMap.pollFirstEntry() != null) {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.baeldung.java.concurrentmodification;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
@@ -28,9 +27,9 @@ public class ConcurrentModificationUnitTest {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
|
||||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext(); ) {
|
||||
Integer integer = iterator.next();
|
||||
if(integer == 2) {
|
||||
if (integer == 2) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
@@ -45,7 +44,7 @@ public class ConcurrentModificationUnitTest {
|
||||
List<Integer> toRemove = newArrayList();
|
||||
|
||||
for (Integer integer : integers) {
|
||||
if(integer == 2) {
|
||||
if (integer == 2) {
|
||||
toRemove.add(integer);
|
||||
}
|
||||
}
|
||||
@@ -69,10 +68,10 @@ public class ConcurrentModificationUnitTest {
|
||||
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
List<String> collected = integers
|
||||
.stream()
|
||||
.filter(i -> i != 2)
|
||||
.map(Object::toString)
|
||||
.collect(toList());
|
||||
.stream()
|
||||
.filter(i -> i != 2)
|
||||
.map(Object::toString)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(collected).containsExactly("1", "3");
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class Java8ExecutorServiceIntegrationTest {
|
||||
|
||||
assertTrue(threadPoolExecutor.isShutdown());
|
||||
assertFalse(notExecutedTasks.isEmpty());
|
||||
assertTrue(notExecutedTasks.size() > 0 && notExecutedTasks.size() < 98);
|
||||
assertTrue(notExecutedTasks.size() < 98);
|
||||
}
|
||||
|
||||
private List<Runnable> smartShutdown(ExecutorService executorService) {
|
||||
|
||||
@@ -23,7 +23,10 @@ public class ThreadPoolInParallelStreamIntegrationTest {
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
|
||||
long actualTotal = customThreadPool
|
||||
.submit(() -> aList.parallelStream()
|
||||
.reduce(0L, Long::sum))
|
||||
.get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user