16 |
|
import java.util.Random; |
17 |
|
import java.util.Set; |
18 |
|
import java.util.concurrent.ConcurrentHashMap; |
19 |
+ |
import java.util.concurrent.ExecutorService; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
|
|
22 |
|
import junit.framework.Test; |
23 |
|
import junit.framework.TestSuite; |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
+ |
* Test keySet().removeAll on empty map |
337 |
+ |
*/ |
338 |
+ |
public void testKeySet_empty_removeAll() { |
339 |
+ |
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); |
340 |
+ |
Set<Integer> set = map.keySet(); |
341 |
+ |
set.removeAll(Collections.emptyList()); |
342 |
+ |
assertTrue(map.isEmpty()); |
343 |
+ |
assertTrue(set.isEmpty()); |
344 |
+ |
// following is test for JDK-8163353 |
345 |
+ |
set.removeAll(Collections.emptySet()); |
346 |
+ |
assertTrue(map.isEmpty()); |
347 |
+ |
assertTrue(set.isEmpty()); |
348 |
+ |
} |
349 |
+ |
|
350 |
+ |
/** |
351 |
|
* keySet.toArray returns contains all keys |
352 |
|
*/ |
353 |
|
public void testKeySetToArray() { |
820 |
|
} |
821 |
|
} |
822 |
|
|
823 |
+ |
/** |
824 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
825 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
826 |
+ |
*/ |
827 |
+ |
public void testRemoveAll_performance() { |
828 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
829 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
830 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
831 |
+ |
for (int i = 0; i < mapSize; i++) |
832 |
+ |
map.put(i, i); |
833 |
+ |
Set<Integer> keySet = map.keySet(); |
834 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
835 |
+ |
for (int i = 0; i < iterations; i++) |
836 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
837 |
+ |
assertEquals(mapSize, map.size()); |
838 |
+ |
} |
839 |
+ |
|
840 |
+ |
/** |
841 |
+ |
* Tests performance of computeIfAbsent when the element is present. |
842 |
+ |
* See JDK-8161372 |
843 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck |
844 |
+ |
*/ |
845 |
+ |
public void testcomputeIfAbsent_performance() { |
846 |
+ |
final int mapSize = 20; |
847 |
+ |
final int iterations = expensiveTests ? (1 << 23) : mapSize * 2; |
848 |
+ |
final int threads = expensiveTests ? 10 : 2; |
849 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
850 |
+ |
for (int i = 0; i < mapSize; i++) |
851 |
+ |
map.put(i, i); |
852 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(2); |
853 |
+ |
try (PoolCleaner cleaner = cleaner(pool)) { |
854 |
+ |
Runnable r = new CheckedRunnable() { |
855 |
+ |
public void realRun() { |
856 |
+ |
int result = 0; |
857 |
+ |
for (int i = 0; i < iterations; i++) |
858 |
+ |
result += map.computeIfAbsent(i % mapSize, (k) -> k + k); |
859 |
+ |
if (result == -42) throw new Error(); |
860 |
+ |
}}; |
861 |
+ |
for (int i = 0; i < threads; i++) |
862 |
+ |
pool.execute(r); |
863 |
+ |
} |
864 |
+ |
} |
865 |
+ |
|
866 |
|
} |