1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4486658 8010293
  27  * @summary thread safety of toArray methods of subCollections
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.concurrent.CompletableFuture;
  32 import java.util.concurrent.ConcurrentHashMap;
  33 import java.util.stream.IntStream;
  34 
  35 public class ToArray {
  36 
  37     public static void main(String[] args) throws Throwable {
  38         // Execute a number of times to increase the probability of
  39         // failure if there is an issue
  40         for (int i = 0; i < 16; i++) {
  41             executeTest();
  42         }
  43     }
  44 
  45     static void executeTest() throws Throwable {
  46         final Throwable[] throwable = new Throwable[1];
  47         final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();
  48 
  49         // Number of workers equal to the number of processors
  50         // Each worker will put globally unique keys into the map
  51         final int nWorkers = Runtime.getRuntime().availableProcessors();
  52         final int sizePerWorker = 1024;
  53         final int maxSize = nWorkers * sizePerWorker;
  54 
  55         // The foreman keeps checking that the size of the arrays
  56         // obtained from the key and value sets is never less than the
  57         // previously observed size and is never greater than the maximum size
  58         // NOTE: these size constraints are not specific to toArray and are
  59         // applicable to any form of traversal of the collection views
  60         CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() {
  61             private int prevSize = 0;
  62 
  63             private boolean checkProgress(Object[] a) {
  64                 int size = a.length;
  65                 if (size < prevSize) throw new RuntimeException("WRONG WAY");
  66                 if (size > maxSize) throw new RuntimeException("OVERSHOOT");
  67                 if (size == maxSize) return true;
  68                 prevSize = size;
  69                 return false;
  70             }
  71 
  72             @Override
  73             public void run() {
  74                 try {
  75                     Integer[] empty = new Integer[0];
  76                     while (true) {
  77                         if (checkProgress(m.values().toArray())) return;
  78                         if (checkProgress(m.keySet().toArray())) return;
  79                         if (checkProgress(m.values().toArray(empty))) return;
  80                         if (checkProgress(m.keySet().toArray(empty))) return;
  81                     }
  82                 }
  83                 catch (Throwable t) {
  84                     throwable[0] = t;
  85                 }
  86             }
  87         });
  88 
  89         // Create workers
  90         // Each worker will put globally unique keys into the map
  91         CompletableFuture<?>[] workers = IntStream.range(0, nWorkers).
  92                 mapToObj(w -> CompletableFuture.runAsync(() -> {
  93                     for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++)
  94                         m.put(o + i, i);
  95                 })).
  96                 toArray(CompletableFuture<?>[]::new);
  97 
  98         // Wait for workers and then foreman to complete
  99         CompletableFuture.allOf(workers).join();
 100         foreman.join();
 101 
 102         if (throwable[0] != null)
 103             throw throwable[0];
 104     }
 105 }