[BAEL-8401] - Added new module core-java-concurrency-collections and moved code and github references from core-java-concurrency module
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
package com.baeldung.concurrent.copyonwrite;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class CopyOnWriteArrayListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCopyOnWriteList_whenIterateAndAddElementToUnderneathList_thenShouldNotChangeIterator() {
|
||||
//given
|
||||
final CopyOnWriteArrayList<Integer> numbers =
|
||||
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
|
||||
|
||||
//when
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
numbers.add(10);
|
||||
|
||||
//then
|
||||
List<Integer> result = new LinkedList<>();
|
||||
iterator.forEachRemaining(result::add);
|
||||
assertThat(result).containsOnly(1, 3, 5, 8);
|
||||
|
||||
//and
|
||||
Iterator<Integer> iterator2 = numbers.iterator();
|
||||
List<Integer> result2 = new LinkedList<>();
|
||||
iterator2.forEachRemaining(result2::add);
|
||||
|
||||
//then
|
||||
assertThat(result2).containsOnly(1, 3, 5, 8, 10);
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenCopyOnWriteList_whenIterateOverItAndTryToRemoveElement_thenShouldThrowException() {
|
||||
//given
|
||||
final CopyOnWriteArrayList<Integer> numbers =
|
||||
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
|
||||
|
||||
//when
|
||||
Iterator<Integer> iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package com.baeldung.concurrent.delayqueue;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.DelayQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class DelayQueueIntegrationTest {
|
||||
@Test
|
||||
public void givenDelayQueue_whenProduceElement_thenShouldConsumeAfterGivenDelay() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
BlockingQueue<DelayObject> queue = new DelayQueue<>();
|
||||
int numberOfElementsToProduce = 2;
|
||||
int delayOfEachProducedMessageMilliseconds = 500;
|
||||
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
|
||||
DelayQueueProducer producer
|
||||
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
|
||||
|
||||
//when
|
||||
executor.submit(producer);
|
||||
executor.submit(consumer);
|
||||
|
||||
//then
|
||||
executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
executor.shutdown();
|
||||
assertEquals(consumer.numberOfConsumedElements.get(), numberOfElementsToProduce);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDelayQueue_whenProduceElementWithHugeDelay_thenConsumerWasNotAbleToConsumeMessageInGivenTime() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
BlockingQueue<DelayObject> queue = new DelayQueue<>();
|
||||
int numberOfElementsToProduce = 1;
|
||||
int delayOfEachProducedMessageMilliseconds = 10_000;
|
||||
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
|
||||
DelayQueueProducer producer
|
||||
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
|
||||
|
||||
//when
|
||||
executor.submit(producer);
|
||||
executor.submit(consumer);
|
||||
|
||||
//then
|
||||
executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
executor.shutdown();
|
||||
assertEquals(consumer.numberOfConsumedElements.get(), 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDelayQueue_whenProduceElementWithNegativeDelay_thenConsumeMessageImmediately() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
BlockingQueue<DelayObject> queue = new DelayQueue<>();
|
||||
int numberOfElementsToProduce = 1;
|
||||
int delayOfEachProducedMessageMilliseconds = -10_000;
|
||||
DelayQueueConsumer consumer = new DelayQueueConsumer(queue, numberOfElementsToProduce);
|
||||
DelayQueueProducer producer
|
||||
= new DelayQueueProducer(queue, numberOfElementsToProduce, delayOfEachProducedMessageMilliseconds);
|
||||
|
||||
//when
|
||||
executor.submit(producer);
|
||||
executor.submit(consumer);
|
||||
|
||||
//then
|
||||
executor.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executor.shutdown();
|
||||
assertEquals(consumer.numberOfConsumedElements.get(), 1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.baeldung.concurrent.locks;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class SharedObjectWithLockManualTest {
|
||||
|
||||
@Test
|
||||
public void whenLockAcquired_ThenLockedIsTrue() {
|
||||
final SharedObjectWithLock object = new SharedObjectWithLock();
|
||||
|
||||
final int threadCount = 2;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
|
||||
executeThreads(object, threadCount, service);
|
||||
|
||||
assertEquals(true, object.isLocked());
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocked_ThenQueuedThread() {
|
||||
final int threadCount = 4;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
final SharedObjectWithLock object = new SharedObjectWithLock();
|
||||
|
||||
executeThreads(object, threadCount, service);
|
||||
|
||||
assertEquals(object.hasQueuedThreads(), true);
|
||||
|
||||
service.shutdown();
|
||||
|
||||
}
|
||||
|
||||
public void whenTryLock_ThenQueuedThread() {
|
||||
final SharedObjectWithLock object = new SharedObjectWithLock();
|
||||
|
||||
final int threadCount = 2;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
|
||||
executeThreads(object, threadCount, service);
|
||||
|
||||
assertEquals(true, object.isLocked());
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetCount_ThenCorrectCount() throws InterruptedException {
|
||||
final int threadCount = 4;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
final SharedObjectWithLock object = new SharedObjectWithLock();
|
||||
|
||||
executeThreads(object, threadCount, service);
|
||||
Thread.sleep(1000);
|
||||
assertEquals(object.getCounter(), 4);
|
||||
|
||||
service.shutdown();
|
||||
|
||||
}
|
||||
|
||||
private void executeThreads(SharedObjectWithLock object, int threadCount, ExecutorService service) {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
service.execute(object::perform);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.concurrent.locks;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class SynchronizedHashMapWithRWLockManualTest {
|
||||
|
||||
@Test
|
||||
public void whenWriting_ThenNoReading() {
|
||||
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
|
||||
final int threadCount = 3;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
|
||||
executeWriterThreads(object, threadCount, service);
|
||||
|
||||
assertEquals(object.isReadLockAvailable(), false);
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReading_ThenMultipleReadingAllowed() {
|
||||
SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock();
|
||||
final int threadCount = 5;
|
||||
final ExecutorService service = Executors.newFixedThreadPool(threadCount);
|
||||
|
||||
executeReaderThreads(object, threadCount, service);
|
||||
|
||||
assertEquals(object.isReadLockAvailable(), true);
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
private void executeWriterThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
service.execute(() -> {
|
||||
try {
|
||||
object.put("key" + threadCount, "value" + threadCount);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) {
|
||||
for (int i = 0; i < threadCount; i++)
|
||||
service.execute(() -> object.get("key" + threadCount));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.baeldung.concurrent.priorityblockingqueue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.util.Lists.newArrayList;
|
||||
|
||||
public class PriorityBlockingQueueIntegrationTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PriorityBlockingQueueIntegrationTest.class);
|
||||
|
||||
|
||||
@Test
|
||||
public void givenUnorderedValues_whenPolling_thenShouldOrderQueue() throws InterruptedException {
|
||||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||
ArrayList<Integer> polledElements = new ArrayList<>();
|
||||
|
||||
queue.add(1);
|
||||
queue.add(5);
|
||||
queue.add(2);
|
||||
queue.add(3);
|
||||
queue.add(4);
|
||||
|
||||
queue.drainTo(polledElements);
|
||||
|
||||
assertThat(polledElements).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPollingEmptyQueue_thenShouldBlockThread() throws InterruptedException {
|
||||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||
|
||||
final Thread thread = new Thread(() -> {
|
||||
LOG.debug("Polling...");
|
||||
while (true) {
|
||||
try {
|
||||
Integer poll = queue.take();
|
||||
LOG.debug("Polled: " + poll);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
||||
LOG.debug("Adding to queue");
|
||||
|
||||
queue.addAll(newArrayList(1, 5, 6, 1, 2, 6, 7));
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package com.baeldung.concurrent.skiplist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentNavigableMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConcurrentSkipListSetIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenThreadsProducingEvents_whenGetForEventsFromLastMinute_thenReturnThoseEventsInTheLockFreeWay() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
EventWindowSort eventWindowSort = new EventWindowSort();
|
||||
int numberOfThreads = 2;
|
||||
//when
|
||||
Runnable producer = () -> IntStream
|
||||
.rangeClosed(0, 100)
|
||||
.forEach(index -> eventWindowSort.acceptEvent(new Event(ZonedDateTime
|
||||
.now()
|
||||
.minusSeconds(index), UUID
|
||||
.randomUUID()
|
||||
.toString())));
|
||||
|
||||
for (int i = 0; i < numberOfThreads; i++) {
|
||||
executorService.execute(producer);
|
||||
}
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
ConcurrentNavigableMap<ZonedDateTime, String> eventsFromLastMinute = eventWindowSort.getEventsFromLastMinute();
|
||||
|
||||
long eventsOlderThanOneMinute = eventsFromLastMinute
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e
|
||||
.getKey()
|
||||
.isBefore(ZonedDateTime
|
||||
.now()
|
||||
.minusMinutes(1)))
|
||||
.count();
|
||||
assertEquals(eventsOlderThanOneMinute, 0);
|
||||
|
||||
long eventYoungerThanOneMinute = eventsFromLastMinute
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e
|
||||
.getKey()
|
||||
.isAfter(ZonedDateTime
|
||||
.now()
|
||||
.minusMinutes(1)))
|
||||
.count();
|
||||
|
||||
//then
|
||||
assertTrue(eventYoungerThanOneMinute > 0);
|
||||
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreadsProducingEvents_whenGetForEventsOlderThanOneMinute_thenReturnThoseEventsInTheLockFreeWay() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
EventWindowSort eventWindowSort = new EventWindowSort();
|
||||
int numberOfThreads = 2;
|
||||
//when
|
||||
Runnable producer = () -> IntStream
|
||||
.rangeClosed(0, 100)
|
||||
.forEach(index -> eventWindowSort.acceptEvent(new Event(ZonedDateTime
|
||||
.now()
|
||||
.minusSeconds(index), UUID
|
||||
.randomUUID()
|
||||
.toString())));
|
||||
|
||||
for (int i = 0; i < numberOfThreads; i++) {
|
||||
executorService.execute(producer);
|
||||
}
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
ConcurrentNavigableMap<ZonedDateTime, String> eventsFromLastMinute = eventWindowSort.getEventsOlderThatOneMinute();
|
||||
|
||||
long eventsOlderThanOneMinute = eventsFromLastMinute
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e
|
||||
.getKey()
|
||||
.isBefore(ZonedDateTime
|
||||
.now()
|
||||
.minusMinutes(1)))
|
||||
.count();
|
||||
assertTrue(eventsOlderThanOneMinute > 0);
|
||||
|
||||
long eventYoungerThanOneMinute = eventsFromLastMinute
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e
|
||||
.getKey()
|
||||
.isAfter(ZonedDateTime
|
||||
.now()
|
||||
.minusMinutes(1)))
|
||||
.count();
|
||||
|
||||
//then
|
||||
assertEquals(eventYoungerThanOneMinute, 0);
|
||||
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class ConcurrentMapAggregateStatusManualTest {
|
||||
|
||||
private ExecutorService executorService;
|
||||
private Map<String, Integer> concurrentMap;
|
||||
private List<Integer> mapSizes;
|
||||
private int MAX_SIZE = 100000;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
executorService = Executors.newFixedThreadPool(2);
|
||||
concurrentMap = new ConcurrentHashMap<>();
|
||||
mapSizes = new ArrayList<>(MAX_SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenSizeWithoutConcurrentUpdates_thenCorrect() throws InterruptedException {
|
||||
Runnable collectMapSizes = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.put(String.valueOf(i), i);
|
||||
mapSizes.add(concurrentMap.size());
|
||||
}
|
||||
};
|
||||
Runnable retrieveMapData = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.get(String.valueOf(i));
|
||||
}
|
||||
};
|
||||
executorService.execute(retrieveMapData);
|
||||
executorService.execute(collectMapSizes);
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
|
||||
for (int i = 1; i <= MAX_SIZE; i++) {
|
||||
assertEquals("map size should be consistently reliable", i, mapSizes.get(i - 1)
|
||||
.intValue());
|
||||
}
|
||||
assertEquals(MAX_SIZE, concurrentMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenUpdatingAndGetSize_thenError() throws InterruptedException {
|
||||
Runnable collectMapSizes = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
mapSizes.add(concurrentMap.size());
|
||||
}
|
||||
};
|
||||
Runnable updateMapData = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.put(String.valueOf(i), i);
|
||||
}
|
||||
};
|
||||
executorService.execute(updateMapData);
|
||||
executorService.execute(collectMapSizes);
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
|
||||
assertNotEquals("map size collected with concurrent updates not reliable", MAX_SIZE, mapSizes.get(MAX_SIZE - 1)
|
||||
.intValue());
|
||||
assertEquals(MAX_SIZE, concurrentMap.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class ConcurrentMapNullKeyValueManualTest {
|
||||
|
||||
private ConcurrentMap<String, Object> concurrentMap;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
concurrentMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.get(null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.getOrDefault(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.put(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() {
|
||||
concurrentMap.put("test", null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.putIfAbsent(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() {
|
||||
Map<String, Object> nullKeyMap = new HashMap<>();
|
||||
nullKeyMap.put(null, new Object());
|
||||
concurrentMap.putAll(nullKeyMap);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() {
|
||||
Map<String, Object> nullValueMap = new HashMap<>();
|
||||
nullValueMap.put("test", null);
|
||||
concurrentMap.putAll(nullValueMap);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() {
|
||||
concurrentMap.replace(null, new Object(), new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() {
|
||||
Object o = new Object();
|
||||
concurrentMap.replace("test", o, null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() {
|
||||
Object o = new Object();
|
||||
concurrentMap.replace("test", null, o);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() {
|
||||
concurrentMap.replace("test", null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() {
|
||||
concurrentMap.replace(null, "test");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() {
|
||||
concurrentMap.put("test", new Object());
|
||||
concurrentMap.replaceAll((s, o) -> null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() {
|
||||
concurrentMap.remove(null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() {
|
||||
concurrentMap.remove(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() {
|
||||
concurrentMap.merge(null, new Object(), (o, o2) -> o2);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() {
|
||||
concurrentMap.put("test", new Object());
|
||||
concurrentMap.merge("test", null, (o, o2) -> o2);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.computeIfAbsent(null, s -> s);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.computeIfPresent(null, (s, o) -> o);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.compute(null, (s, o) -> o);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.merge("test", new Object(), (o, o2) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() {
|
||||
concurrentMap.computeIfPresent("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.computeIfPresent("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.compute("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConcurrentMapPerformanceManualTest {
|
||||
|
||||
@Test
|
||||
public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception {
|
||||
final Map<String, Object> hashtable = new Hashtable<>();
|
||||
final Map<String, Object> synchronizedHashMap = Collections.synchronizedMap(new HashMap<>());
|
||||
final Map<String, Object> concurrentHashMap = new ConcurrentHashMap<>();
|
||||
|
||||
final long hashtableAvgRuntime = timeElapseForGetPut(hashtable);
|
||||
final long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap);
|
||||
final long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap);
|
||||
|
||||
System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime));
|
||||
|
||||
assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime);
|
||||
assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime);
|
||||
|
||||
}
|
||||
|
||||
private long timeElapseForGetPut(Map<String, Object> map) throws InterruptedException {
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
final long startTime = System.nanoTime();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < 500_000; j++) {
|
||||
final int value = ThreadLocalRandom.current().nextInt(10000);
|
||||
final String key = String.valueOf(value);
|
||||
map.put(key, value);
|
||||
map.get(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
return (System.nanoTime() - startTime) / 500_000;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenKeyWithSameHashCode_thenPerformanceDegrades() throws InterruptedException {
|
||||
class SameHash {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
final int executeTimes = 5000;
|
||||
|
||||
final Map<SameHash, Integer> mapOfSameHash = new ConcurrentHashMap<>();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
final long sameHashStartTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < executeTimes; j++) {
|
||||
mapOfSameHash.put(new SameHash(), 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
|
||||
final long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime;
|
||||
final Map<Object, Integer> mapOfDefaultHash = new ConcurrentHashMap<>();
|
||||
executorService = Executors.newFixedThreadPool(2);
|
||||
final long defaultHashStartTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < executeTimes; j++) {
|
||||
mapOfDefaultHash.put(new Object(), 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
|
||||
final long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime;
|
||||
assertEquals(executeTimes * 2, mapOfDefaultHash.size());
|
||||
assertEquals(executeTimes * 2, mapOfSameHash.size());
|
||||
System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration));
|
||||
assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > (mapOfDefaultHashDuration * 10));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NavigableMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConcurrentNavigableMapManualTest {
|
||||
|
||||
@Test
|
||||
public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException {
|
||||
NavigableMap<Integer, String> skipListMap = new ConcurrentSkipListMap<>();
|
||||
|
||||
updateMapConcurrently(skipListMap);
|
||||
|
||||
Iterator<Integer> skipListIter = skipListMap.keySet().iterator();
|
||||
int previous = skipListIter.next();
|
||||
while (skipListIter.hasNext()) {
|
||||
int current = skipListIter.next();
|
||||
assertTrue(previous < current);
|
||||
}
|
||||
}
|
||||
|
||||
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++) {
|
||||
navigableMap.put(random.nextInt(), "test");
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSkipListMap_whenNavConcurrently_thenCountCorrect() throws InterruptedException {
|
||||
NavigableMap<Integer, Integer> skipListMap = new ConcurrentSkipListMap<>();
|
||||
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);
|
||||
assertNotEquals(10000 * 4, count);
|
||||
}
|
||||
|
||||
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(4);
|
||||
for (int j = 0; j < 4; j++) {
|
||||
executorService.execute(() -> {
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
if (navigableMap.pollFirstEntry() != null) {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
return counter.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ConcurretMapMemoryConsistencyManualTest {
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenSumParallel_thenCorrect() throws Exception {
|
||||
Map<String, Integer> map = new ConcurrentHashMap<>();
|
||||
List<Integer> sumList = parallelSum100(map, 1000);
|
||||
assertEquals(1, sumList.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertEquals(0, wrongResultCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashtable_whenSumParallel_thenCorrect() throws Exception {
|
||||
Map<String, Integer> map = new Hashtable<>();
|
||||
List<Integer> sumList = parallelSum100(map, 1000);
|
||||
assertEquals(1, sumList.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertEquals(0, wrongResultCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashMap_whenSumParallel_thenError() throws Exception {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
List<Integer> sumList = parallelSum100(map, 100);
|
||||
assertNotEquals(1, sumList.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertTrue(wrongResultCount > 0);
|
||||
}
|
||||
|
||||
private List<Integer> parallelSum100(Map<String, Integer> map, int executionTimes) throws InterruptedException {
|
||||
List<Integer> sumList = new ArrayList<>(1000);
|
||||
for (int i = 0; i < executionTimes; i++) {
|
||||
map.put("test", 0);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
for (int j = 0; j < 10; j++) {
|
||||
executorService.execute(() -> {
|
||||
for (int k = 0; k < 10; k++)
|
||||
map.computeIfPresent("test", (key, value) -> value + 1);
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
sumList.add(map.get("test"));
|
||||
}
|
||||
return sumList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.baeldung.java.concurrentmodification;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.util.Lists.newArrayList;
|
||||
|
||||
public class ConcurrentModificationUnitTest {
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
for (Integer integer : integers) {
|
||||
integers.remove(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext(); ) {
|
||||
Integer integer = iterator.next();
|
||||
if (integer == 2) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
List<Integer> toRemove = newArrayList();
|
||||
|
||||
for (Integer integer : integers) {
|
||||
if (integer == 2) {
|
||||
toRemove.add(integer);
|
||||
}
|
||||
}
|
||||
integers.removeAll(toRemove);
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException {
|
||||
|
||||
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
integers.removeIf(i -> i == 2);
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStream_thenRemoveElements() {
|
||||
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
List<String> collected = integers
|
||||
.stream()
|
||||
.filter(i -> i != 2)
|
||||
.map(Object::toString)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(collected).containsExactly("1", "3");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package com.baeldung.synchronousqueue;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class SynchronousQueueIntegrationTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SynchronousQueueIntegrationTest.class);
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoThreads_whenWantToExchangeUsingLockGuardedVariable_thenItSucceed() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
AtomicInteger sharedState = new AtomicInteger();
|
||||
CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
|
||||
Runnable producer = () -> {
|
||||
Integer producedElement = ThreadLocalRandom.current().nextInt();
|
||||
LOG.debug("Saving an element: " + producedElement + " to the exchange point");
|
||||
sharedState.set(producedElement);
|
||||
countDownLatch.countDown();
|
||||
};
|
||||
|
||||
Runnable consumer = () -> {
|
||||
try {
|
||||
countDownLatch.await();
|
||||
Integer consumedElement = sharedState.get();
|
||||
LOG.debug("consumed an element: " + consumedElement + " from the exchange point");
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
//when
|
||||
executor.execute(producer);
|
||||
executor.execute(consumer);
|
||||
|
||||
//then
|
||||
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||
executor.shutdown();
|
||||
assertEquals(countDownLatch.getCount(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoThreads_whenWantToExchangeUsingSynchronousQueue_thenItSucceed() throws InterruptedException {
|
||||
//given
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
final SynchronousQueue<Integer> queue = new SynchronousQueue<>();
|
||||
|
||||
Runnable producer = () -> {
|
||||
Integer producedElement = ThreadLocalRandom.current().nextInt();
|
||||
try {
|
||||
LOG.debug("Saving an element: " + producedElement + " to the exchange point");
|
||||
queue.put(producedElement);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
Runnable consumer = () -> {
|
||||
try {
|
||||
Integer consumedElement = queue.take();
|
||||
LOG.debug("consumed an element: " + consumedElement + " from the exchange point");
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
//when
|
||||
executor.execute(producer);
|
||||
executor.execute(consumer);
|
||||
|
||||
//then
|
||||
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||
executor.shutdown();
|
||||
assertEquals(queue.size(), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.baeldung.transferqueue;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TransferQueueIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenMultipleConsumersAndProducers_thenProcessAllMessages() throws InterruptedException {
|
||||
//given
|
||||
TransferQueue<String> transferQueue = new LinkedTransferQueue<>();
|
||||
ExecutorService exService = Executors.newFixedThreadPool(3);
|
||||
Producer producer1 = new Producer(transferQueue, "1", 3);
|
||||
Producer producer2 = new Producer(transferQueue, "2", 3);
|
||||
Consumer consumer1 = new Consumer(transferQueue, "1", 3);
|
||||
Consumer consumer2 = new Consumer(transferQueue, "2", 3);
|
||||
|
||||
//when
|
||||
exService.execute(producer1);
|
||||
exService.execute(producer2);
|
||||
exService.execute(consumer1);
|
||||
exService.execute(consumer2);
|
||||
|
||||
//then
|
||||
exService.awaitTermination(5000, TimeUnit.MILLISECONDS);
|
||||
exService.shutdown();
|
||||
|
||||
assertEquals(producer1.numberOfProducedMessages.intValue(), 3);
|
||||
assertEquals(producer2.numberOfProducedMessages.intValue(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseOneConsumerAndOneProducer_thenShouldProcessAllMessages() throws InterruptedException {
|
||||
//given
|
||||
TransferQueue<String> transferQueue = new LinkedTransferQueue<>();
|
||||
ExecutorService exService = Executors.newFixedThreadPool(2);
|
||||
Producer producer = new Producer(transferQueue, "1", 3);
|
||||
Consumer consumer = new Consumer(transferQueue, "1", 3);
|
||||
|
||||
//when
|
||||
exService.execute(producer);
|
||||
exService.execute(consumer);
|
||||
|
||||
//then
|
||||
exService.awaitTermination(5000, TimeUnit.MILLISECONDS);
|
||||
exService.shutdown();
|
||||
|
||||
assertEquals(producer.numberOfProducedMessages.intValue(), 3);
|
||||
assertEquals(consumer.numberOfConsumedMessages.intValue(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseOneProducerAndNoConsumers_thenShouldFailWithTimeout() throws InterruptedException {
|
||||
//given
|
||||
TransferQueue<String> transferQueue = new LinkedTransferQueue<>();
|
||||
ExecutorService exService = Executors.newFixedThreadPool(2);
|
||||
Producer producer = new Producer(transferQueue, "1", 3);
|
||||
|
||||
//when
|
||||
exService.execute(producer);
|
||||
|
||||
//then
|
||||
exService.awaitTermination(5000, TimeUnit.MILLISECONDS);
|
||||
exService.shutdown();
|
||||
|
||||
assertEquals(producer.numberOfProducedMessages.intValue(), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package org.baeldung.java.streams;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadPoolInParallelStreamIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
|
||||
long firstNum = 1;
|
||||
long lastNum = 1_000_000;
|
||||
|
||||
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();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
|
||||
List<Long> aList = new ArrayList<>();
|
||||
Stream<Long> parallelStream = aList.parallelStream();
|
||||
|
||||
assertTrue(parallelStream.isParallel());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user