jdk/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
permissions -rw-r--r--
8150523: improve jtreg test timeout handling, especially -timeout: Reviewed-by: martin, psandoz, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11832
diff changeset
     2
 * Copyright (c) 2005, 2012, 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 6277663
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test TPE extensibility framework
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    28
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    32
import static java.util.concurrent.TimeUnit.MILLISECONDS;
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    33
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    34
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    35
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    36
import java.util.concurrent.FutureTask;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    37
import java.util.concurrent.RunnableFuture;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    38
import java.util.concurrent.RunnableScheduledFuture;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    39
import java.util.concurrent.ScheduledThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    40
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    41
import java.util.concurrent.TimeUnit;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    42
import java.util.concurrent.atomic.AtomicInteger;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    43
import java.util.function.BooleanSupplier;
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    44
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Custom {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    47
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void pass() { passed++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static void fail() { failed++; Thread.dumpStack(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void check(boolean cond) { if (cond) pass(); else fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        else {System.out.println(x + " not equal to " + y); fail(); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static class CustomTask<V> extends FutureTask<V> {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    58
        public static final AtomicInteger births = new AtomicInteger(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static class CustomTPE extends ThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        CustomTPE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            super(threadCount, threadCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                  30, TimeUnit.MILLISECONDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                  new ArrayBlockingQueue<Runnable>(2*threadCount));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return new CustomTask<V>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return new CustomTask<V>(r, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static class CustomSTPE extends ScheduledThreadPoolExecutor {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    78
        public static final AtomicInteger decorations = new AtomicInteger(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        CustomSTPE() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            super(threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Runnable r, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            decorations.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            Callable<V> c, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            decorations.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static int countExecutorThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Thread[] threads = new Thread[Thread.activeCount()+100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Thread.enumerate(threads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (Thread t : threads)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   104
    private static final int threadCount = 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   106
    static long millisElapsedSince(long startTime) {
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   107
        return (System.nanoTime() - startTime) / (1000L * 1000L);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   108
    }
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   109
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   110
    static void spinWaitUntil(BooleanSupplier predicate, long timeoutMillis) {
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   111
        long startTime = -1L;
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   112
        while (!predicate.getAsBoolean()) {
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   113
            if (startTime == -1L)
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   114
                startTime = System.nanoTime();
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   115
            else if (millisElapsedSince(startTime) > timeoutMillis)
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   116
                throw new AssertionError(
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   117
                    String.format("timed out after %s ms", timeoutMillis));
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   118
            Thread.yield();
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   119
        }
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   120
    }
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        CustomTPE tpe = new CustomTPE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        equal(tpe.getCorePoolSize(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        equal(countExecutorThreads(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < threadCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            tpe.submit(new Runnable() { public void run() {}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        equal(countExecutorThreads(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        equal(CustomTask.births.get(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        tpe.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   131
        tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   132
        spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        CustomSTPE stpe = new CustomSTPE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (int i = 0; i < threadCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            stpe.submit(new Runnable() { public void run() {}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        equal(CustomSTPE.decorations.get(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        equal(countExecutorThreads(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        stpe.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   140
        stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   141
        spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}