RFR 8010293 <was> Re: Potential issue with CHM.toArray

Paul Sandoz paul.sandoz at oracle.com
Mon Sep 9 13:35:14 UTC 2013

On Sep 6, 2013, at 4:56 PM, Alan Bateman <Alan.Bateman at oracle.com> wrote:

> On 06/09/2013 12:08, Paul Sandoz wrote:
>> On Sep 2, 2013, at 3:24 PM, Paul Sandoz<Paul.Sandoz at oracle.com>  wrote:
>> :
>>> Here is the fix in the lambda repo which can be applied to tl:
>>>  http://hg.openjdk.java.net/lambda/lambda/jdk/rev/b73937e96ae0
>>>  http://hg.openjdk.java.net/lambda/lambda/jdk/raw-rev/b73937e96ae0
>> Anyone up for reviewing this?
> The comments are very educational as the resizing is difficult to completely grok without going through examples on a whiteboard. Anyway, I don't see anything obviously wrong after going through it. The test case is useful although creating the list of threads is quite a mouth full to take in.

Yeah, i left that in a convoluted intermediate state and wanted to use CountedCompleter instead, see below for a revised and preferred version.


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;

public class toArray {

    public static void main(String[] args) throws Throwable {
        // Execute a number of times to increase the probability of
        // failure if there is an issue
        for (int i = 0; i < 16; i++) {

    static void executeTest() throws Throwable {
        final Throwable throwable[] = new Throwable[1];
        final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();

        // Number of workers equal to the number of processors
        final int nWorkers = Runtime.getRuntime().availableProcessors();
        final int sizePerWorker = 1024;
        final int maxSize = nWorkers * sizePerWorker;

        // The foreman keeps checking that the size of the arrays
        // obtained from the key and value sets is never less than the
        // previously observed size and is never greater than the maximum size
        // NOTE: these size constraints are not specific to toArray and are
        // applicable to any form of traversal of the collection views
        CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() {
            private int prevSize = 0;

            private boolean checkProgress(Object[] a) {
                int size = a.length;
                if (size < prevSize) throw new RuntimeException("WRONG WAY");
                if (size > maxSize) throw new RuntimeException("OVERSHOOT");
                if (size == maxSize) return true;
                prevSize = size;
                return false;

            public void run() {
                try {
                    Integer[] empty = new Integer[0];
                    while (true) {
                        if (checkProgress(m.values().toArray())) return;
                        if (checkProgress(m.keySet().toArray())) return;
                        if (checkProgress(m.values().toArray(empty))) return;
                        if (checkProgress(m.keySet().toArray(empty))) return;
                catch (Throwable t) {
                    throwable[0] = t;

        // Create workers
        // Each worker will put globally unique keys into the map
        CompletableFuture<?>[] workers = IntStream.range(0, nWorkers).
                mapToObj(w -> CompletableFuture.runAsync(() -> {
                    for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++)
                        m.put(o + i, i);

        // Wait for workers and then foreman to complete

        if (throwable[0] != null)
            throw throwable[0];

More information about the core-libs-dev mailing list