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