test/jdk/java/util/concurrent/FutureTask/BlockingTaskExecutor.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43522 jdk/test/java/util/concurrent/FutureTask/BlockingTaskExecutor.java@f9c6f543c4db
child 48541 946e34c2dec9
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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: 11830
diff changeset
     2
 * Copyright (c) 2006, 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 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    31
import java.util.ArrayList;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    32
import java.util.Collection;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    33
import java.util.concurrent.Callable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    34
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    35
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    36
import java.util.concurrent.RejectedExecutionException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    37
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Adapted from Doug Lea, which was...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * adapted from a posting by Tom Sugden tom at epcc.ed.ac.uk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class BlockingTaskExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        for (int i = 1; i <= 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            System.out.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void test() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final ExecutorService executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final NotificationReceiver notifiee1 = new NotificationReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final NotificationReceiver notifiee2 = new NotificationReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 34347
diff changeset
    58
        final Collection<Callable<Object>> tasks = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        tasks.add(new BlockingTask(notifiee1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        tasks.add(new BlockingTask(notifiee2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        tasks.add(new NonBlockingTask());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // start a thread to invoke the tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Thread thread = new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            try { executor.invokeAll(tasks); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            catch (RejectedExecutionException t) {/* OK */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            catch (Throwable t) { unexpected(t); }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Wait until tasks begin execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        notifiee1.waitForNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        notifiee2.waitForNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // Now try to shutdown the executor service while tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // are blocked.  This should cause the tasks to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        executor.shutdownNow();
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 14342
diff changeset
    78
        if (! executor.awaitTermination(5L, TimeUnit.SECONDS))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new Error("Executor stuck");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Wait for the invocation thread to complete.
11830
c92191a9447c 7105929: java/util/concurrent/FutureTask/BlockingTaskExecutor.java fails on solaris sparc
chegar
parents: 7668
diff changeset
    82
        thread.join(5000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (thread.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            thread.interrupt();
11830
c92191a9447c 7105929: java/util/concurrent/FutureTask/BlockingTaskExecutor.java fails on solaris sparc
chegar
parents: 7668
diff changeset
    85
            thread.join(5000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new Error("invokeAll stuck");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * A helper class with a method to wait for a notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The notification is received via the
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    94
     * {@code sendNotification} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static class NotificationReceiver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /** Has the notifiee been notified? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        boolean notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * Notify the notification receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public synchronized void sendNotification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * Waits until a notification has been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * @throws InterruptedException if the wait is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        public synchronized void waitForNotification()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            while (! notified)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * A callable task that blocks until it is interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This task sends a notification to a notification receiver when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * it is first called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static class BlockingTask implements Callable<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private final NotificationReceiver notifiee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        BlockingTask(NotificationReceiver notifiee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            this.notifiee = notifiee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public Object call() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            notifiee.sendNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // wait indefinitely until task is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * A callable task that simply returns a string result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static class NonBlockingTask implements Callable<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public Object call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return "NonBlockingTaskResult";
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
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}