|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
|
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 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6576792 |
|
27 * @summary non-idle worker threads should not be interrupted |
|
28 */ |
|
29 |
|
30 import java.util.concurrent.*; |
|
31 |
|
32 public class SelfInterrupt { |
|
33 void test(String[] args) throws Throwable { |
|
34 final int n = 100; |
|
35 final ThreadPoolExecutor pool = |
|
36 new ThreadPoolExecutor(n, n, 1L, TimeUnit.NANOSECONDS, |
|
37 new SynchronousQueue<Runnable>()); |
|
38 final CountDownLatch startingGate = new CountDownLatch(n); |
|
39 final CountDownLatch finishLine = new CountDownLatch(n); |
|
40 equal(pool.getCorePoolSize(), n); |
|
41 equal(pool.getPoolSize(), 0); |
|
42 for (int i = 0; i < n; i++) |
|
43 pool.execute(new Runnable() { public void run() { |
|
44 try { |
|
45 startingGate.countDown(); |
|
46 startingGate.await(); |
|
47 equal(pool.getPoolSize(), n); |
|
48 pool.setCorePoolSize(n); |
|
49 pool.setCorePoolSize(1); |
|
50 check(! Thread.interrupted()); |
|
51 equal(pool.getPoolSize(), n); |
|
52 finishLine.countDown(); |
|
53 finishLine.await(); |
|
54 check(! Thread.interrupted()); |
|
55 } catch (Throwable t) { unexpected(t); }}}); |
|
56 finishLine.await(); |
|
57 pool.shutdown(); |
|
58 check(pool.awaitTermination(1000L, TimeUnit.SECONDS)); |
|
59 } |
|
60 |
|
61 //--------------------- Infrastructure --------------------------- |
|
62 volatile int passed = 0, failed = 0; |
|
63 void pass() {passed++;} |
|
64 void fail() {failed++; Thread.dumpStack();} |
|
65 void fail(String msg) {System.err.println(msg); fail();} |
|
66 void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
67 void check(boolean cond) {if (cond) pass(); else fail();} |
|
68 void equal(Object x, Object y) { |
|
69 if (x == null ? y == null : x.equals(y)) pass(); |
|
70 else fail(x + " not equal to " + y);} |
|
71 public static void main(String[] args) throws Throwable { |
|
72 new SelfInterrupt().instanceMain(args);} |
|
73 void instanceMain(String[] args) throws Throwable { |
|
74 try {test(args);} catch (Throwable t) {unexpected(t);} |
|
75 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
76 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
77 } |