author | dl |
Fri, 03 Feb 2017 13:24:59 -0800 | |
changeset 43522 | f9c6f543c4db |
parent 34347 | 4a17f9e90a0f |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6431315 |
|
27 |
* @summary ExecutorService.invokeAll might hang |
|
28 |
* @author Martin Buchholz |
|
29 |
*/ |
|
30 |
||
34347 | 31 |
import java.util.ArrayList; |
32 |
import java.util.Collection; |
|
33 |
import java.util.concurrent.Callable; |
|
34 |
import java.util.concurrent.ExecutorService; |
|
35 |
import java.util.concurrent.Executors; |
|
36 |
import java.util.concurrent.RejectedExecutionException; |
|
37 |
import java.util.concurrent.TimeUnit; |
|
2 | 38 |
|
39 |
/** |
|
40 |
* Adapted from Doug Lea, which was... |
|
41 |
* adapted from a posting by Tom Sugden tom at epcc.ed.ac.uk |
|
42 |
*/ |
|
43 |
public class BlockingTaskExecutor { |
|
44 |
||
45 |
static void realMain(String[] args) throws Throwable { |
|
46 |
for (int i = 1; i <= 100; i++) { |
|
47 |
System.out.print("."); |
|
48 |
test(); |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
static void test() throws Throwable { |
|
53 |
final ExecutorService executor = Executors.newCachedThreadPool(); |
|
54 |
||
55 |
final NotificationReceiver notifiee1 = new NotificationReceiver(); |
|
56 |
final NotificationReceiver notifiee2 = new NotificationReceiver(); |
|
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 | 59 |
tasks.add(new BlockingTask(notifiee1)); |
60 |
tasks.add(new BlockingTask(notifiee2)); |
|
61 |
tasks.add(new NonBlockingTask()); |
|
62 |
||
63 |
// start a thread to invoke the tasks |
|
64 |
Thread thread = new Thread() { public void run() { |
|
65 |
try { executor.invokeAll(tasks); } |
|
66 |
catch (RejectedExecutionException t) {/* OK */} |
|
67 |
catch (Throwable t) { unexpected(t); }}}; |
|
68 |
thread.start(); |
|
69 |
||
70 |
// Wait until tasks begin execution |
|
71 |
notifiee1.waitForNotification(); |
|
72 |
notifiee2.waitForNotification(); |
|
73 |
||
74 |
// Now try to shutdown the executor service while tasks |
|
75 |
// are blocked. This should cause the tasks to be |
|
76 |
// interrupted. |
|
77 |
executor.shutdownNow(); |
|
34347 | 78 |
if (! executor.awaitTermination(5L, TimeUnit.SECONDS)) |
2 | 79 |
throw new Error("Executor stuck"); |
80 |
||
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 | 83 |
if (thread.isAlive()) { |
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 | 86 |
throw new Error("invokeAll stuck"); |
87 |
} |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* A helper class with a method to wait for a notification. |
|
92 |
* |
|
93 |
* The notification is received via the |
|
7518 | 94 |
* {@code sendNotification} method. |
2 | 95 |
*/ |
96 |
static class NotificationReceiver { |
|
97 |
/** Has the notifiee been notified? */ |
|
98 |
boolean notified = false; |
|
99 |
||
100 |
/** |
|
101 |
* Notify the notification receiver. |
|
102 |
*/ |
|
103 |
public synchronized void sendNotification() { |
|
104 |
notified = true; |
|
105 |
notifyAll(); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Waits until a notification has been received. |
|
110 |
* |
|
111 |
* @throws InterruptedException if the wait is interrupted |
|
112 |
*/ |
|
113 |
public synchronized void waitForNotification() |
|
114 |
throws InterruptedException { |
|
115 |
while (! notified) |
|
116 |
wait(); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* A callable task that blocks until it is interrupted. |
|
122 |
* This task sends a notification to a notification receiver when |
|
123 |
* it is first called. |
|
124 |
*/ |
|
125 |
static class BlockingTask implements Callable<Object> { |
|
126 |
private final NotificationReceiver notifiee; |
|
127 |
||
128 |
BlockingTask(NotificationReceiver notifiee) { |
|
129 |
this.notifiee = notifiee; |
|
130 |
} |
|
131 |
||
132 |
public Object call() throws InterruptedException { |
|
133 |
notifiee.sendNotification(); |
|
134 |
||
135 |
// wait indefinitely until task is interrupted |
|
136 |
while (true) { |
|
137 |
synchronized (this) { |
|
138 |
wait(); |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* A callable task that simply returns a string result. |
|
146 |
*/ |
|
147 |
static class NonBlockingTask implements Callable<Object> { |
|
148 |
public Object call() { |
|
149 |
return "NonBlockingTaskResult"; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
//--------------------- Infrastructure --------------------------- |
|
154 |
static volatile int passed = 0, failed = 0; |
|
155 |
static void pass() {passed++;} |
|
156 |
static void fail() {failed++; Thread.dumpStack();} |
|
157 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
158 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
159 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
160 |
static void equal(Object x, Object y) { |
|
161 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
162 |
else fail(x + " not equal to " + y);} |
|
163 |
public static void main(String[] args) throws Throwable { |
|
164 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
165 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
166 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
167 |
} |