test/jdk/java/util/concurrent/FutureTask/BlockingTaskExecutor.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 51769 0ae80830256e
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51769
0ae80830256e 8210779: 8182404 and 8210732 haven't updated copyright years
iignatyev
parents: 51754
diff changeset
     2
 * Copyright (c) 2006, 2018, 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 6431315
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary ExecutorService.invokeAll might hang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 48541
diff changeset
    29
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    32
import static java.util.concurrent.TimeUnit.MILLISECONDS;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    33
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    34
import java.util.ArrayList;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    35
import java.util.Collection;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    36
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    37
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    38
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    39
import java.util.concurrent.RejectedExecutionException;
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 48541
diff changeset
    40
import jdk.test.lib.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Adapted from Doug Lea, which was...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * adapted from a posting by Tom Sugden tom at epcc.ed.ac.uk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BlockingTaskExecutor {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    47
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i = 1; i <= 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.out.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static void test() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        final ExecutorService executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        final NotificationReceiver notifiee1 = new NotificationReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        final NotificationReceiver notifiee2 = new NotificationReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 34347
diff changeset
    62
        final Collection<Callable<Object>> tasks = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        tasks.add(new BlockingTask(notifiee1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        tasks.add(new BlockingTask(notifiee2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        tasks.add(new NonBlockingTask());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // start a thread to invoke the tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Thread thread = new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try { executor.invokeAll(tasks); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            catch (RejectedExecutionException t) {/* OK */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            catch (Throwable t) { unexpected(t); }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // Wait until tasks begin execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        notifiee1.waitForNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        notifiee2.waitForNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Now try to shutdown the executor service while tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // are blocked.  This should cause the tasks to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        executor.shutdownNow();
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    82
        if (! executor.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    83
            throw new Error(
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    84
                String.format("Executor termination timed out after %d ms",
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    85
                              LONG_DELAY_MS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Wait for the invocation thread to complete.
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    88
        thread.join(LONG_DELAY_MS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (thread.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            thread.interrupt();
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    91
            thread.join(LONG_DELAY_MS);
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    92
            throw new Error(
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    93
                String.format("invokeAll timed out after %d ms",
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    94
                              LONG_DELAY_MS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * A helper class with a method to wait for a notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The notification is received via the
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   102
     * {@code sendNotification} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static class NotificationReceiver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /** Has the notifiee been notified? */
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 51769
diff changeset
   106
        boolean notified;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * Notify the notification receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public synchronized void sendNotification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * Waits until a notification has been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * @throws InterruptedException if the wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public synchronized void waitForNotification()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            while (! notified)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A callable task that blocks until it is interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * This task sends a notification to a notification receiver when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * it is first called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static class BlockingTask implements Callable<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        private final NotificationReceiver notifiee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        BlockingTask(NotificationReceiver notifiee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            this.notifiee = notifiee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public Object call() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            notifiee.sendNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // wait indefinitely until task is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * A callable task that simply returns a string result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static class NonBlockingTask implements Callable<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public Object call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return "NonBlockingTaskResult";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}