jdk/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java
author iignatyev
Tue, 30 May 2017 21:14:45 -0700
changeset 45288 58be10a068c2
parent 43522 f9c6f543c4db
child 45466 faf1c55d2046
permissions -rw-r--r--
8180805: move RandomFactory to the top level testlibrary Reviewed-by: bpb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 43522
diff changeset
     2
 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6450200
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test proper handling of pool state changes
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 43522
diff changeset
    28
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/othervm ConfigChanges
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
    30
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    34
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    35
import static java.util.concurrent.TimeUnit.MINUTES;
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
    36
import static java.util.concurrent.TimeUnit.NANOSECONDS;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    37
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    38
import java.security.Permission;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    39
import java.util.Random;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    40
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    41
import java.util.concurrent.CyclicBarrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    42
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    43
import java.util.concurrent.RejectedExecutionException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    44
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    45
import java.util.concurrent.ThreadPoolExecutor;
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
    46
import java.util.function.Supplier;
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 43522
diff changeset
    47
import jdk.test.lib.RandomFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ConfigChanges {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    50
    static final ThreadGroup tg = new ThreadGroup("pool");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    52
    static final Random rnd = RandomFactory.getRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void report(ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.out.printf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                "active=%d submitted=%d completed=%d queued=%d sizes=%d/%d/%d%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                tg.activeCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                tpe.getTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                tpe.getCompletedTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                tpe.getQueue().size(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                tpe.getPoolSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                tpe.getCorePoolSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static void report(String label, ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.printf("%10s ", label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        report(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static class PermissiveSecurityManger extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static void checkShutdown(final ExecutorService es) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        final Runnable nop = new Runnable() {public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (new Random().nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                check(es.isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (es instanceof ThreadPoolExecutor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    check(((ThreadPoolExecutor) es).isTerminating()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                          || es.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                THROWS(RejectedExecutionException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    86
                       () -> es.execute(nop));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static void checkTerminated(final ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            checkShutdown(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            check(tpe.getQueue().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            check(tpe.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            check(! tpe.isTerminating());
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
    97
            equal(0, tpe.getActiveCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
    98
            equal(0, tpe.getPoolSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   100
            check(tpe.awaitTermination(0L, MINUTES));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static Runnable waiter(final CyclicBarrier barrier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return new Runnable() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            try { barrier.await(); barrier.await(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            catch (Throwable t) { unexpected(t); }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static volatile Runnable runnableDuJour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   112
    static void awaitIdleness(ThreadPoolExecutor tpe, long taskCount) {
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   113
        restart: for (;;) {
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   114
            // check twice to make chance of race vanishingly small
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   115
            for (int i = 0; i < 2; i++) {
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   116
                if (tpe.getQueue().size() != 0 ||
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   117
                    tpe.getActiveCount() != 0 ||
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   118
                    tpe.getCompletedTaskCount() != taskCount) {
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   119
                    Thread.yield();
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   120
                    continue restart;
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   121
                }
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   122
            }
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   123
            return;
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   124
        }
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   125
    }
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   126
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   127
    /**
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   128
     * Waits for condition to become true, first spin-polling, then sleep-polling.
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   129
     */
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   130
    static void spinAwait(Supplier<Boolean> waitingForGodot) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   131
        for (int spins = 0; !waitingForGodot.get(); ) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   132
            if ((spins = (spins + 1) & 3) > 0) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   133
                Thread.yield();
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   134
            } else {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   135
                try { Thread.sleep(4); }
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   136
                catch (InterruptedException unexpected) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   137
                    throw new AssertionError(unexpected);
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   138
                }
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   139
            }
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   140
        }
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   141
    }
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.setSecurityManager(new PermissiveSecurityManger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        final boolean prestart = rnd.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        final Thread.UncaughtExceptionHandler handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            = new Thread.UncaughtExceptionHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    public void uncaughtException(Thread t, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        check(! Thread.currentThread().isInterrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        final int n = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        final ThreadPoolExecutor tpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            = new ThreadPoolExecutor(n, 3*n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                     3L, MINUTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                     new ArrayBlockingQueue<Runnable>(3*n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        tpe.setThreadFactory(new ThreadFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    Thread t = new Thread(tg, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    t.setUncaughtExceptionHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (prestart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            tpe.prestartAllCoreThreads();
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   170
            equal(n, tg.activeCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   171
            equal(n, tpe.getCorePoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   172
            equal(n, tpe.getLargestPoolSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        final Runnable runRunnableDuJour =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            new Runnable() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // Delay choice of action till last possible moment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                runnableDuJour.run(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        final CyclicBarrier pumpedUp = new CyclicBarrier(3*n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        runnableDuJour = waiter(pumpedUp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (prestart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            for (int i = 0; i < 1*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // Wait for prestarted threads to dequeue their initial tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            while (! tpe.getQueue().isEmpty())
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   187
                Thread.sleep(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            for (int i = 0; i < 5*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            for (int i = 0; i < 6*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        //report("submitted", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        pumpedUp.await();
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   197
        equal(3*n, tg.activeCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   198
        equal(3*n, tpe.getMaximumPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   199
        equal(3*n, tpe.getLargestPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   200
        equal(n, tpe.getCorePoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   201
        equal(3*n, tpe.getActiveCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   202
        equal(6L*n, tpe.getTaskCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   203
        equal(0L, tpe.getCompletedTaskCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   204
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        //report("pumped up", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        tpe.setMaximumPoolSize(4*n);
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   207
        equal(4*n, tpe.getMaximumPoolSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //report("pumped up2", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        final CyclicBarrier pumpedUp2 = new CyclicBarrier(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        runnableDuJour = waiter(pumpedUp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (int i = 0; i < 1*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        pumpedUp2.await();
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   214
        equal(4*n, tg.activeCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   215
        equal(4*n, tpe.getMaximumPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   216
        equal(4*n, tpe.getLargestPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   217
        equal(4*n, tpe.getActiveCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   218
        equal(7L*n, tpe.getTaskCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   219
        equal(0L, tpe.getCompletedTaskCount());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        //report("pumped up2", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        runnableDuJour = new Runnable() { public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        tpe.setMaximumPoolSize(2*n);
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   224
        //report("after setMaximumPoolSize", tpe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        pumpedUp2.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        pumpedUp.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   229
        spinAwait(() -> tg.activeCount() == 2*n);
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   230
        equal(2*n, tpe.getMaximumPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   231
        equal(4*n, tpe.getLargestPoolSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   233
        //report("draining", tpe);
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   234
        awaitIdleness(tpe, 7L*n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   236
        equal(2*n, tg.activeCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   237
        equal(2*n, tpe.getMaximumPoolSize());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   238
        equal(4*n, tpe.getLargestPoolSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   240
        equal(7L*n, tpe.getTaskCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   241
        equal(7L*n, tpe.getCompletedTaskCount());
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   242
        equal(0, tpe.getActiveCount());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   244
        equal(3L, tpe.getKeepAliveTime(MINUTES));
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   245
        long t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        tpe.setKeepAliveTime(7L, MILLISECONDS);
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   247
        equal(7L, tpe.getKeepAliveTime(MILLISECONDS));
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   248
        spinAwait(() -> tg.activeCount() == n);
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   249
        check(System.nanoTime() - t0 >= tpe.getKeepAliveTime(NANOSECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //report("idle", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        check(! tpe.allowsCoreThreadTimeOut());
40816
9756dfcba32e 8139237: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java fails intermittently
dl
parents: 34516
diff changeset
   253
        t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        tpe.allowCoreThreadTimeOut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        check(tpe.allowsCoreThreadTimeOut());
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42318
diff changeset
   256
        spinAwait(() -> tg.activeCount() == 0);
42318
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   257
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   258
        // The following assertion is almost always true, but may
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   259
        // exceptionally not be during a transition from core count
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   260
        // too high to allowCoreThreadTimeOut.  Users will never
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   261
        // notice, and we accept the small loss of testability.
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   262
        //
35806bea1e90 8169243: java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java still fails intermittently
dl
parents: 40816
diff changeset
   263
        // check(System.nanoTime() - t0 >= tpe.getKeepAliveTime(NANOSECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        //report("idle", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        checkShutdown(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        check(tpe.awaitTermination(3L, MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   287
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}