|
| 1 | +package com.baeldung.java.concurrentmap; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.NavigableMap; |
| 7 | +import java.util.TreeMap; |
| 8 | +import java.util.concurrent.*; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | + |
| 11 | +import static org.testng.Assert.*; |
| 12 | + |
| 13 | +public class ConcurrentNavigableMapTests { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException { |
| 17 | + NavigableMap<Integer, String> skipListMap = new ConcurrentSkipListMap<>(); |
| 18 | + |
| 19 | + updateMapConcurrently(skipListMap, 4); |
| 20 | + |
| 21 | + Iterator<Integer> skipListIter = skipListMap |
| 22 | + .keySet() |
| 23 | + .iterator(); |
| 24 | + int previous = skipListIter.next(); |
| 25 | + while (skipListIter.hasNext()) { |
| 26 | + int current = skipListIter.next(); |
| 27 | + assertTrue(previous < current); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void updateMapConcurrently(NavigableMap<Integer, String> navigableMap, int concurrencyLevel) throws InterruptedException { |
| 32 | + ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel); |
| 33 | + for (int i = 0; i < concurrencyLevel; i++) { |
| 34 | + executorService.execute(() -> { |
| 35 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 36 | + for (int j = 0; j < 10000; j++) { |
| 37 | + navigableMap.put(random.nextInt(), "test"); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + executorService.shutdown(); |
| 42 | + executorService.awaitTermination(1, TimeUnit.MINUTES); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenSkipListMap_whenNavConcurrently_thenCountCorrect() throws InterruptedException { |
| 47 | + NavigableMap<Integer, Integer> skipListMap = new ConcurrentSkipListMap<>(); |
| 48 | + int count = countMapElementByPollingFirstEntry(skipListMap, 10000, 4); |
| 49 | + assertEquals(10000 * 4, count); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenTreeMap_whenNavConcurrently_thenCountError() throws InterruptedException { |
| 54 | + NavigableMap<Integer, Integer> treeMap = new TreeMap<>(); |
| 55 | + int count = countMapElementByPollingFirstEntry(treeMap, 10000, 4); |
| 56 | + assertNotEquals(10000 * 4, count); |
| 57 | + } |
| 58 | + |
| 59 | + private int countMapElementByPollingFirstEntry(NavigableMap<Integer, Integer> navigableMap, int elementCount, int concurrencyLevel) throws InterruptedException { |
| 60 | + for (int i = 0; i < elementCount * concurrencyLevel; i++) { |
| 61 | + navigableMap.put(i, i); |
| 62 | + } |
| 63 | + AtomicInteger counter = new AtomicInteger(0); |
| 64 | + ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel); |
| 65 | + for (int j = 0; j < concurrencyLevel; j++) { |
| 66 | + executorService.execute(() -> { |
| 67 | + for (int i = 0; i < elementCount; i++) { |
| 68 | + if (navigableMap.pollFirstEntry() != null) { |
| 69 | + counter.incrementAndGet(); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + executorService.shutdown(); |
| 75 | + executorService.awaitTermination(1, TimeUnit.MINUTES); |
| 76 | + return counter.get(); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments