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; |
47 |
|
} |
48 |
|
|
49 |
|
/** Re-implement Integer.compare for old java versions */ |
50 |
< |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
50 |
> |
static int compare(int x, int y) { |
51 |
> |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
52 |
> |
} |
53 |
|
|
54 |
|
// classes for testing Comparable fallbacks |
55 |
|
static class BI implements Comparable<BI> { |
537 |
|
/** |
538 |
|
* Constructor (initialCapacity, loadFactor) throws |
539 |
|
* IllegalArgumentException if either argument is negative |
540 |
< |
*/ |
540 |
> |
*/ |
541 |
|
public void testConstructor2() { |
542 |
|
try { |
543 |
|
new ConcurrentHashMap(-1, .75f); |
805 |
|
} |
806 |
|
} |
807 |
|
|
808 |
+ |
/** |
809 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
810 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
811 |
+ |
*/ |
812 |
+ |
public void testRemoveAll_performance() { |
813 |
+ |
final int mapSize = expensiveTests ? 1_000_000 : 100; |
814 |
+ |
final int iterations = expensiveTests ? 500 : 2; |
815 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
816 |
+ |
for (int i = 0; i < mapSize; i++) |
817 |
+ |
map.put(i, i); |
818 |
+ |
Set<Integer> keySet = map.keySet(); |
819 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
820 |
+ |
for (int i = 0; i < iterations; i++) |
821 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
822 |
+ |
assertEquals(mapSize, map.size()); |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
/** |
826 |
+ |
* Tests performance of computeIfAbsent when the element is present. |
827 |
+ |
* See JDK-8161372 |
828 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck |
829 |
+ |
*/ |
830 |
+ |
public void testcomputeIfAbsent_performance() { |
831 |
+ |
final int mapSize = 20; |
832 |
+ |
final int iterations = expensiveTests ? (1 << 23) : mapSize * 2; |
833 |
+ |
final int threads = expensiveTests ? 10 : 2; |
834 |
+ |
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
835 |
+ |
for (int i = 0; i < mapSize; i++) |
836 |
+ |
map.put(i, i); |
837 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(2); |
838 |
+ |
try (PoolCleaner cleaner = cleaner(pool)) { |
839 |
+ |
Runnable r = new CheckedRunnable() { |
840 |
+ |
public void realRun() { |
841 |
+ |
int result = 0; |
842 |
+ |
for (int i = 0; i < iterations; i++) |
843 |
+ |
result += map.computeIfAbsent(i % mapSize, (k) -> k + k); |
844 |
+ |
if (result == -42) throw new Error(); |
845 |
+ |
}}; |
846 |
+ |
for (int i = 0; i < threads; i++) |
847 |
+ |
pool.execute(r); |
848 |
+ |
} |
849 |
+ |
} |
850 |
+ |
|
851 |
|
} |