author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 7976 | f273c0d04215 |
child 10119 | 277fd8d9fcda |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7976
diff
changeset
|
2 |
* Copyright (c) 2006, 2011, 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 6399443 |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7668
diff
changeset
|
27 |
* @run main/othervm AutoShutdown |
2 | 28 |
* @summary Check for auto-shutdown and gc of singleThreadExecutors |
29 |
* @author Martin Buchholz |
|
30 |
*/ |
|
31 |
||
32 |
import java.io.*; |
|
33 |
import java.util.*; |
|
34 |
import java.util.concurrent.*; |
|
35 |
import static java.util.concurrent.Executors.*; |
|
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
36 |
import java.util.concurrent.Phaser; |
2 | 37 |
|
38 |
public class AutoShutdown { |
|
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
39 |
private static void waitForFinalizersToRun() { |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
40 |
for (int i = 0; i < 2; i++) |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
41 |
tryWaitForFinalizersToRun(); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
42 |
} |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
43 |
|
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
44 |
private static void tryWaitForFinalizersToRun() { |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
45 |
System.gc(); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
46 |
final CountDownLatch fin = new CountDownLatch(1); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
47 |
new Object() { protected void finalize() { fin.countDown(); }}; |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
48 |
System.gc(); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
49 |
try { fin.await(); } |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
50 |
catch (InterruptedException ie) { throw new Error(ie); } |
2 | 51 |
} |
52 |
||
53 |
private static void realMain(String[] args) throws Throwable { |
|
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
54 |
final Phaser phaser = new Phaser(3); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
55 |
Runnable trivialRunnable = new Runnable() { |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
56 |
public void run() { |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
57 |
phaser.arriveAndAwaitAdvance(); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
58 |
} |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
59 |
}; |
2 | 60 |
int count0 = Thread.activeCount(); |
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
61 |
Executor e1 = newSingleThreadExecutor(); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
62 |
Executor e2 = newSingleThreadExecutor(defaultThreadFactory()); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
63 |
e1.execute(trivialRunnable); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
64 |
e2.execute(trivialRunnable); |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
65 |
phaser.arriveAndAwaitAdvance(); |
2 | 66 |
equal(Thread.activeCount(), count0 + 2); |
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
67 |
e1 = e2 = null; |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
68 |
for (int i = 0; i < 10 && Thread.activeCount() > count0; i++) |
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
69 |
tryWaitForFinalizersToRun(); |
2 | 70 |
equal(Thread.activeCount(), count0); |
71 |
} |
|
72 |
||
73 |
//--------------------- Infrastructure --------------------------- |
|
74 |
static volatile int passed = 0, failed = 0; |
|
75 |
static void pass() {passed++;} |
|
76 |
static void fail() {failed++; Thread.dumpStack();} |
|
77 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
78 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
79 |
static void equal(Object x, Object y) { |
|
80 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
81 |
else fail(x + " not equal to " + y);} |
|
82 |
public static void main(String[] args) throws Throwable { |
|
83 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
84 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
85 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
86 |
} |