jdk/src/java.base/share/classes/java/util/concurrent/Helpers.java
author dl
Tue, 13 Oct 2015 16:45:35 -0700
changeset 32991 b27c76b82713
permissions -rw-r--r--
8134853: Bulk integration of java.util.concurrent classes 8080939: ForkJoinPool and Phaser deadlock 8044616: Clients of Unsafe.compareAndSwapLong need to beware of using direct stores to the same field 8071638: [JAVADOC] Buggy example in javadoc for afterExecute to access a submitted job's Throwable 8043743: Data missed in java.util.concurrent.LinkedTransferQueue 8054446: Repeated offer and remove on ConcurrentLinkedQueue lead to an OutOfMemoryError 8031374: TEST_BUG: java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java fails Intermittently 8034208: Cleanup to test/java/util/concurrent/BlockingQueue/Interrupt.java 8035661: Test fix java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java from jsr166 CVS 8062841: ConcurrentHashMap.computeIfAbsent stuck in an endless loop 8073208: javadoc typo in java.util.concurrent.Executor 8073704: FutureTask.isDone returns true when task has not yet completed 8037093: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails intermittently 8022642: ScheduledThreadPoolExecutor with zero corePoolSize create endlessly threads 8065320: Busy loop in ThreadPoolExecutor.getTask for ScheduledThreadPoolExecutor 8129861: High processor load for ScheduledThreadPoolExecutor with 0 core threads 8051859: ScheduledExecutorService.scheduleWithFixedDelay fails with max delay 7146994: example afterExecute for ScheduledThreadPoolExecutor hangs Reviewed-by: martin, psandoz, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     1
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     3
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     9
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    14
 * accompanied this code).
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    15
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    19
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    22
 * questions.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    23
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    24
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    25
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    29
 * file:
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    30
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    31
 * Written by Martin Buchholz with assistance from members of JCP
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    32
 * JSR-166 Expert Group and released to the public domain, as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    33
 * explained at http://creativecommons.org/publicdomain/zero/1.0/
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    34
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    35
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    36
package java.util.concurrent;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    37
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    38
import java.util.Collection;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    39
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    40
/** Shared implementation code for java.util.concurrent. */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    41
class Helpers {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    42
    private Helpers() {}                // non-instantiable
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    43
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    44
    /**
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    45
     * An implementation of Collection.toString() suitable for classes
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    46
     * with locks.  Instead of holding a lock for the entire duration of
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    47
     * toString(), or acquiring a lock for each call to Iterator.next(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    48
     * we hold the lock only during the call to toArray() (less
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    49
     * disruptive to other threads accessing the collection) and follows
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    50
     * the maxim "Never call foreign code while holding a lock".
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    51
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    52
    static String collectionToString(Collection<?> c) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    53
        final Object[] a = c.toArray();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    54
        final int size = a.length;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    55
        if (size == 0)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    56
            return "[]";
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    57
        int charLength = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    58
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    59
        // Replace every array element with its string representation
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    60
        for (int i = 0; i < size; i++) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    61
            Object e = a[i];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    62
            // Extreme compatibility with AbstractCollection.toString()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    63
            String s = (e == c) ? "(this Collection)" : objectToString(e);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    64
            a[i] = s;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    65
            charLength += s.length();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    66
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    67
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    68
        return toString(a, size, charLength);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    69
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    70
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    71
    /**
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    72
     * Like Arrays.toString(), but caller guarantees that size > 0,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    73
     * each element with index 0 <= i < size is a non-null String,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    74
     * and charLength is the sum of the lengths of the input Strings.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    75
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    76
    static String toString(Object[] a, int size, int charLength) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    77
        // assert a != null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    78
        // assert size > 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    79
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    80
        // Copy each string into a perfectly sized char[]
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    81
        // Length of [ , , , ] == 2 * size
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    82
        final char[] chars = new char[charLength + 2 * size];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    83
        chars[0] = '[';
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    84
        int j = 1;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    85
        for (int i = 0; i < size; i++) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    86
            if (i > 0) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    87
                chars[j++] = ',';
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    88
                chars[j++] = ' ';
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    89
            }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    90
            String s = (String) a[i];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    91
            int len = s.length();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    92
            s.getChars(0, len, chars, j);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    93
            j += len;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    94
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    95
        chars[j] = ']';
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    96
        // assert j == chars.length - 1;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    97
        return new String(chars);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    98
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    99
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   100
    /** Optimized form of: key + "=" + val */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   101
    static String mapEntryToString(Object key, Object val) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   102
        final String k, v;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   103
        final int klen, vlen;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   104
        final char[] chars =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   105
            new char[(klen = (k = objectToString(key)).length()) +
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   106
                     (vlen = (v = objectToString(val)).length()) + 1];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   107
        k.getChars(0, klen, chars, 0);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   108
        chars[klen] = '=';
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   109
        v.getChars(0, vlen, chars, klen + 1);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   110
        return new String(chars);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   111
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   112
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   113
    private static String objectToString(Object x) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   114
        // Extreme compatibility with StringBuilder.append(null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   115
        String s;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   116
        return (x == null || (s = x.toString()) == null) ? "null" : s;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   117
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   118
}