< prev index next >

test/java/util/concurrent/ConcurrentHashMap/ConcurrentAssociateTest.java

Print this page




 103     private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
 104         ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
 105         CountDownLatch s = new CountDownLatch(1);
 106 
 107         Supplier<Runnable> sr = () -> () -> {
 108             try {
 109                 s.await();
 110             }
 111             catch (InterruptedException e) {
 112             }
 113 
 114             for (int i = 0; i < N; i++) {
 115                 Object o = new X();
 116                 associator.accept(m, o);
 117                 if (!m.containsKey(o)) {
 118                     throw new AssociationFailure(desc + " failed: entry does not exist");
 119                 }
 120             }
 121         };
 122 
 123         int ps = Runtime.getRuntime().availableProcessors();

 124         Stream<CompletableFuture> runners = IntStream.range(0, ps)
 125                 .mapToObj(i -> sr.get())
 126                 .map(CompletableFuture::runAsync);
 127 
 128         CompletableFuture all = CompletableFuture.allOf(
 129                 runners.toArray(CompletableFuture[]::new));
 130 
 131         // Trigger the runners to start associating
 132         s.countDown();
 133         try {
 134             all.join();
 135         } catch (CompletionException e) {
 136             Throwable t = e.getCause();
 137             if (t instanceof AssociationFailure) {
 138                 throw (AssociationFailure) t;
 139             }
 140             else {
 141                 throw e;
 142             }
 143         }


 103     private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
 104         ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
 105         CountDownLatch s = new CountDownLatch(1);
 106 
 107         Supplier<Runnable> sr = () -> () -> {
 108             try {
 109                 s.await();
 110             }
 111             catch (InterruptedException e) {
 112             }
 113 
 114             for (int i = 0; i < N; i++) {
 115                 Object o = new X();
 116                 associator.accept(m, o);
 117                 if (!m.containsKey(o)) {
 118                     throw new AssociationFailure(desc + " failed: entry does not exist");
 119                 }
 120             }
 121         };
 122 
 123         // Bound concurrency to avoid degenerate performance
 124         int ps = Math.min(Runtime.getRuntime().availableProcessors(), 32);
 125         Stream<CompletableFuture> runners = IntStream.range(0, ps)
 126                 .mapToObj(i -> sr.get())
 127                 .map(CompletableFuture::runAsync);
 128 
 129         CompletableFuture all = CompletableFuture.allOf(
 130                 runners.toArray(CompletableFuture[]::new));
 131 
 132         // Trigger the runners to start associating
 133         s.countDown();
 134         try {
 135             all.join();
 136         } catch (CompletionException e) {
 137             Throwable t = e.getCause();
 138             if (t instanceof AssociationFailure) {
 139                 throw (AssociationFailure) t;
 140             }
 141             else {
 142                 throw e;
 143             }
 144         }
< prev index next >