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 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.
22 */
23
24 /*
25 * @test
26 * @bug 6450200 6450205 6450207 6450211
27 * @summary Test proper handling of tasks that terminate abruptly
28 * @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
29 * @author Martin Buchholz
30 */
31
32 import java.security.*;
33 import java.util.*;
34 import java.util.concurrent.*;
35 import java.util.concurrent.atomic.*;
36
37 public class ThrowingTasks {
38 static final Random rnd = new Random();
39
40 @SuppressWarnings("serial")
41 static class UncaughtExceptions
42 extends ConcurrentHashMap<Class<?>, Integer> {
43
44 void inc(Class<?> key) {
45 for (;;) {
46 Integer i = get(key);
47 if (i == null) {
48 if (putIfAbsent(key, 1) == null)
49 return;
50 } else {
51 if (replace(key, i, i + 1))
52 return;
53 }
54 }
55 }
139 static final CountDownLatch allContinue = new CountDownLatch(1);
140
141 static class PermissiveSecurityManger extends SecurityManager {
142 public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
143 }
144
145 static void checkTerminated(ThreadPoolExecutor tpe) {
146 try {
147 check(tpe.getQueue().isEmpty());
148 check(tpe.isShutdown());
149 check(tpe.isTerminated());
150 check(! tpe.isTerminating());
151 equal(tpe.getActiveCount(), 0);
152 equal(tpe.getPoolSize(), 0);
153 equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
154 check(tpe.awaitTermination(0, TimeUnit.SECONDS));
155 } catch (Throwable t) { unexpected(t); }
156 }
157
158 static class CheckingExecutor extends ThreadPoolExecutor {
159 CheckingExecutor() {
160 super(10, 10,
161 1L, TimeUnit.HOURS,
162 new LinkedBlockingQueue<Runnable>(),
163 tf);
164 }
165 @Override protected void beforeExecute(Thread t, Runnable r) {
166 allStarted.countDown();
167 if (allStarted.getCount() < getCorePoolSize())
168 try { allContinue.await(); }
169 catch (InterruptedException x) { unexpected(x); }
170 beforeExecuteCount.getAndIncrement();
171 check(! isTerminated());
172 ((Flaky)r).beforeExecute.run();
173 }
174 @Override protected void afterExecute(Runnable r, Throwable t) {
175 //System.out.println(tg.activeCount());
176 afterExecuteCount.getAndIncrement();
177 check(((Thrower)((Flaky)r).execute).t == t);
178 check(! isTerminated());
179 }
180 @Override protected void terminated() {
181 try {
182 terminatedCount.getAndIncrement();
183 if (rnd.nextBoolean()) {
184 check(isShutdown());
185 check(isTerminating());
186 check(! isTerminated());
187 check(! awaitTermination(0L, TimeUnit.MINUTES));
|
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 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.
22 */
23
24 /*
25 * @test
26 * @bug 6450200 6450205 6450207 6450211
27 * @summary Test proper handling of tasks that terminate abruptly
28 * @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
29 * @author Martin Buchholz
30 */
31
32 import java.security.*;
33 import java.util.*;
34 import java.util.concurrent.*;
35 import java.util.concurrent.atomic.*;
36 import java.util.concurrent.locks.ReentrantLock;
37
38 public class ThrowingTasks {
39 static final Random rnd = new Random();
40
41 @SuppressWarnings("serial")
42 static class UncaughtExceptions
43 extends ConcurrentHashMap<Class<?>, Integer> {
44
45 void inc(Class<?> key) {
46 for (;;) {
47 Integer i = get(key);
48 if (i == null) {
49 if (putIfAbsent(key, 1) == null)
50 return;
51 } else {
52 if (replace(key, i, i + 1))
53 return;
54 }
55 }
56 }
140 static final CountDownLatch allContinue = new CountDownLatch(1);
141
142 static class PermissiveSecurityManger extends SecurityManager {
143 public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
144 }
145
146 static void checkTerminated(ThreadPoolExecutor tpe) {
147 try {
148 check(tpe.getQueue().isEmpty());
149 check(tpe.isShutdown());
150 check(tpe.isTerminated());
151 check(! tpe.isTerminating());
152 equal(tpe.getActiveCount(), 0);
153 equal(tpe.getPoolSize(), 0);
154 equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
155 check(tpe.awaitTermination(0, TimeUnit.SECONDS));
156 } catch (Throwable t) { unexpected(t); }
157 }
158
159 static class CheckingExecutor extends ThreadPoolExecutor {
160 private ReentrantLock lock = new ReentrantLock();
161 CheckingExecutor() {
162 super(10, 10,
163 1L, TimeUnit.HOURS,
164 new LinkedBlockingQueue<Runnable>(),
165 tf);
166 }
167 @Override protected void beforeExecute(Thread t, Runnable r) {
168 boolean lessThanCorePoolSize = false;
169 //Add a lock to sync allStarted.countDown() and
170 //allStarted.getCount() < getCorePoolSize()
171 lock.lock();
172 try{
173 allStarted.countDown();
174 lessThanCorePoolSize = allStarted.getCount() < getCorePoolSize();
175 } finally {
176 lock.unlock();
177 }
178 if (lessThanCorePoolSize)
179 try { allContinue.await(); }
180 catch (InterruptedException x) { unexpected(x); }
181 beforeExecuteCount.getAndIncrement();
182 check(! isTerminated());
183 ((Flaky)r).beforeExecute.run();
184 }
185 @Override protected void afterExecute(Runnable r, Throwable t) {
186 //System.out.println(tg.activeCount());
187 afterExecuteCount.getAndIncrement();
188 check(((Thrower)((Flaky)r).execute).t == t);
189 check(! isTerminated());
190 }
191 @Override protected void terminated() {
192 try {
193 terminatedCount.getAndIncrement();
194 if (rnd.nextBoolean()) {
195 check(isShutdown());
196 check(isTerminating());
197 check(! isTerminated());
198 check(! awaitTermination(0L, TimeUnit.MINUTES));
|