Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ThreadPoolExecutor/ScheduledTickleService.java
+++ new/test/java/util/concurrent/ThreadPoolExecutor/ScheduledTickleService.java
1 1 /*
2 2 * Copyright (c) 2006, 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 6362121
27 27 * @summary Test one ScheduledThreadPoolExecutor extension scenario
28 28 * @author Martin Buchholz
29 29 */
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
30 30
31 31 // based on a test kindly provided by Holger Hoffstaette <holger@wizards.de>
32 32
33 33 import java.util.concurrent.*;
34 34 import static java.util.concurrent.TimeUnit.MILLISECONDS;
35 35
36 36 public class ScheduledTickleService {
37 37
38 38 // We get intermittent ClassCastException if greater than 1
39 39 // because of calls to compareTo
40 - private final static int concurrency = 2;
40 + private static final int concurrency = 2;
41 41
42 42 // Record when tasks are done
43 - public final static CountDownLatch done = new CountDownLatch(concurrency);
43 + public static final CountDownLatch done = new CountDownLatch(concurrency);
44 44
45 45 public static void realMain(String... args) throws InterruptedException {
46 46 // our tickle service
47 47 ScheduledExecutorService tickleService =
48 48 new ScheduledThreadPoolExecutor(concurrency) {
49 49 // We override decorateTask() to return a custom
50 50 // RunnableScheduledFuture which explicitly removes
51 51 // itself from the queue after cancellation.
52 52 protected <V> RunnableScheduledFuture<V>
53 53 decorateTask(Runnable runnable,
54 54 RunnableScheduledFuture<V> task) {
55 55 final ScheduledThreadPoolExecutor exec = this;
56 56 return new CustomRunnableScheduledFuture<V>(task) {
57 57 // delegate to wrapped task, except for:
58 58 public boolean cancel(boolean b) {
59 59 // cancel wrapped task & remove myself from the queue
60 60 return (task().cancel(b)
61 61 && exec.remove(this));}};}};
62 62
63 63 for (int i = 0; i < concurrency; i++)
64 64 new ScheduledTickle(i, tickleService)
65 65 .setUpdateInterval(25, MILLISECONDS);
66 66
67 67 done.await();
68 68 tickleService.shutdown();
69 69 pass();
70 70 }
71 71
72 72 // our Runnable
73 73 static class ScheduledTickle implements Runnable {
74 74 public volatile int failures = 0;
75 75
76 76 // my tickle service
77 77 private final ScheduledExecutorService service;
78 78
79 79 // remember my own scheduled ticket
80 80 private ScheduledFuture ticket = null;
81 81
82 82 // remember the number of times I've been tickled
83 83 private int numTickled = 0;
84 84
85 85 // my private name
86 86 private final String name;
87 87
88 88 public ScheduledTickle(int i, ScheduledExecutorService service) {
89 89 super();
90 90 this.name = "Tickler-"+i;
91 91 this.service = service;
92 92 }
93 93
94 94 // set my tickle interval; 0 to disable further tickling.
95 95 public synchronized void setUpdateInterval(long interval,
96 96 TimeUnit unit) {
97 97 // cancel & remove previously created ticket
98 98 if (ticket != null) {
99 99 ticket.cancel(false);
100 100 ticket = null;
101 101 }
102 102
103 103 if (interval > 0 && ! service.isShutdown()) {
104 104 // requeue with new interval
105 105 ticket = service.scheduleAtFixedRate(this, interval,
106 106 interval, unit);
107 107 }
108 108 }
109 109
110 110 public synchronized void run() {
111 111 try {
112 112 check(numTickled < 6);
113 113 numTickled++;
114 114 System.out.println(name + ": Run " + numTickled);
115 115
116 116 // tickle 3 times and then slow down
117 117 if (numTickled == 3) {
118 118 System.out.println(name + ": slower please!");
119 119 this.setUpdateInterval(100, MILLISECONDS);
120 120 }
121 121 // ..but only 5 times max.
122 122 else if (numTickled == 5) {
123 123 System.out.println(name + ": OK that's enough.");
124 124 this.setUpdateInterval(0, MILLISECONDS);
125 125 ScheduledTickleService.done.countDown();
126 126 }
127 127 } catch (Throwable t) { unexpected(t); }
128 128 }
129 129 }
130 130
131 131 // This is just a generic wrapper to make up for the private ScheduledFutureTask
132 132 static class CustomRunnableScheduledFuture<V>
133 133 implements RunnableScheduledFuture<V> {
134 134 // the wrapped future
135 135 private RunnableScheduledFuture<V> task;
136 136
137 137 public CustomRunnableScheduledFuture(RunnableScheduledFuture<V> task) {
138 138 super();
139 139 this.task = task;
140 140 }
141 141
142 142 public RunnableScheduledFuture<V> task() { return task; }
143 143
144 144 // Forwarding methods
145 145 public boolean isPeriodic() { return task.isPeriodic(); }
146 146 public boolean isCancelled() { return task.isCancelled(); }
147 147 public boolean isDone() { return task.isDone(); }
148 148 public boolean cancel(boolean b) { return task.cancel(b); }
149 149 public long getDelay(TimeUnit unit) { return task.getDelay(unit); }
150 150 public void run() { task.run(); }
151 151
152 152 public V get()
153 153 throws InterruptedException, ExecutionException {
154 154 return task.get();
155 155 }
156 156
157 157 public V get(long timeout, TimeUnit unit)
158 158 throws InterruptedException, ExecutionException, TimeoutException {
159 159 return task.get(timeout, unit);
160 160 }
161 161
162 162 public int compareTo(Delayed other) {
163 163 if (this == other)
164 164 return 0;
165 165 else if (other instanceof CustomRunnableScheduledFuture)
166 166 return task.compareTo(((CustomRunnableScheduledFuture)other).task());
167 167 else
168 168 return task.compareTo(other);
169 169 }
170 170 }
171 171
172 172 //--------------------- Infrastructure ---------------------------
173 173 static volatile int passed = 0, failed = 0;
174 174 static void pass() {passed++;}
175 175 static void fail() {failed++; Thread.dumpStack();}
176 176 static void fail(String msg) {System.out.println(msg); fail();}
177 177 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
178 178 static void check(boolean cond) {if (cond) pass(); else fail();}
179 179 static void equal(Object x, Object y) {
180 180 if (x == null ? y == null : x.equals(y)) pass();
181 181 else fail(x + " not equal to " + y);}
182 182 public static void main(String[] args) throws Throwable {
183 183 try {realMain(args);} catch (Throwable t) {unexpected(t);}
184 184 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
185 185 if (failed > 0) throw new AssertionError("Some tests failed");}
186 186 }
↓ open down ↓ |
133 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX