Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
+++ new/test/java/util/concurrent/ThreadPoolExecutor/Custom.java
1 1 /*
2 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6277663
27 27 * @summary Test TPE extensibility framework
28 28 * @author Martin Buchholz
29 29 */
30 30
31 31 import java.util.concurrent.*;
32 32 import java.util.concurrent.atomic.*;
33 33
34 34 public class Custom {
35 35 static volatile int passed = 0, failed = 0;
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
36 36 static void pass() { passed++; }
37 37 static void fail() { failed++; Thread.dumpStack(); }
38 38 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
39 39 static void check(boolean cond) { if (cond) pass(); else fail(); }
40 40 static void equal(Object x, Object y) {
41 41 if (x == null ? y == null : x.equals(y)) pass();
42 42 else {System.out.println(x + " not equal to " + y); fail(); }}
43 43
44 44
45 45 private static class CustomTask<V> extends FutureTask<V> {
46 - public final static AtomicInteger births = new AtomicInteger(0);
46 + public static final AtomicInteger births = new AtomicInteger(0);
47 47 CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); }
48 48 CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); }
49 49 }
50 50
51 51 private static class CustomTPE extends ThreadPoolExecutor {
52 52 CustomTPE() {
53 53 super(threadCount, threadCount,
54 54 30, TimeUnit.MILLISECONDS,
55 55 new ArrayBlockingQueue<Runnable>(2*threadCount));
56 56 }
57 57 protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
58 58 return new CustomTask<V>(c);
59 59 }
60 60 protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
61 61 return new CustomTask<V>(r, v);
62 62 }
63 63 }
64 64
65 65 private static class CustomSTPE extends ScheduledThreadPoolExecutor {
66 - public final static AtomicInteger decorations = new AtomicInteger(0);
66 + public static final AtomicInteger decorations = new AtomicInteger(0);
67 67 CustomSTPE() {
68 68 super(threadCount);
69 69 }
70 70 protected <V> RunnableScheduledFuture<V> decorateTask(
71 71 Runnable r, RunnableScheduledFuture<V> task) {
72 72 decorations.getAndIncrement();
73 73 return task;
74 74 }
75 75 protected <V> RunnableScheduledFuture<V> decorateTask(
76 76 Callable<V> c, RunnableScheduledFuture<V> task) {
77 77 decorations.getAndIncrement();
78 78 return task;
79 79 }
80 80 }
81 81
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
82 82 static int countExecutorThreads() {
83 83 Thread[] threads = new Thread[Thread.activeCount()+100];
84 84 Thread.enumerate(threads);
85 85 int count = 0;
86 86 for (Thread t : threads)
87 87 if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
88 88 count++;
89 89 return count;
90 90 }
91 91
92 - private final static int threadCount = 10;
92 + private static final int threadCount = 10;
93 93
94 94 public static void main(String[] args) throws Throwable {
95 95 CustomTPE tpe = new CustomTPE();
96 96 equal(tpe.getCorePoolSize(), threadCount);
97 97 equal(countExecutorThreads(), 0);
98 98 for (int i = 0; i < threadCount; i++)
99 99 tpe.submit(new Runnable() { public void run() {}});
100 100 equal(countExecutorThreads(), threadCount);
101 101 equal(CustomTask.births.get(), threadCount);
102 102 tpe.shutdown();
103 103 tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
104 104 Thread.sleep(10);
105 105 equal(countExecutorThreads(), 0);
106 106
107 107 CustomSTPE stpe = new CustomSTPE();
108 108 for (int i = 0; i < threadCount; i++)
109 109 stpe.submit(new Runnable() { public void run() {}});
110 110 equal(CustomSTPE.decorations.get(), threadCount);
111 111 equal(countExecutorThreads(), threadCount);
112 112 stpe.shutdown();
113 113 stpe.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
114 114 Thread.sleep(10);
115 115 equal(countExecutorThreads(), 0);
116 116
117 117 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
118 118 if (failed > 0) throw new Exception("Some tests failed");
119 119 }
120 120 }
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX