|
Java example source code file (ThrowingTasks.java)
The ThrowingTasks.java Java example source code
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6450200 6450205 6450207 6450211
* @summary Test proper handling of tasks that terminate abruptly
* @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
* @author Martin Buchholz
*/
import java.security.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.ReentrantLock;
public class ThrowingTasks {
static final Random rnd = new Random();
@SuppressWarnings("serial")
static class UncaughtExceptions
extends ConcurrentHashMap<Class>, Integer> {
void inc(Class<?> key) {
for (;;) {
Integer i = get(key);
if (i == null) {
if (putIfAbsent(key, 1) == null)
return;
} else {
if (replace(key, i, i + 1))
return;
}
}
}
}
@SuppressWarnings("serial")
static class UncaughtExceptionsTable
extends Hashtable<Class>, Integer> {
synchronized void inc(Class<?> key) {
Integer i = get(key);
put(key, (i == null) ? 1 : i + 1);
}
}
static final UncaughtExceptions uncaughtExceptions
= new UncaughtExceptions();
static final UncaughtExceptionsTable uncaughtExceptionsTable
= new UncaughtExceptionsTable();
static final AtomicLong totalUncaughtExceptions
= new AtomicLong(0);
static final CountDownLatch uncaughtExceptionsLatch
= new CountDownLatch(24);
static final Thread.UncaughtExceptionHandler handler
= new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
check(! Thread.currentThread().isInterrupted());
totalUncaughtExceptions.getAndIncrement();
uncaughtExceptions.inc(e.getClass());
uncaughtExceptionsTable.inc(e.getClass());
uncaughtExceptionsLatch.countDown();
}};
static final ThreadGroup tg = new ThreadGroup("Flaky");
static final ThreadFactory tf = new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(tg, r);
t.setUncaughtExceptionHandler(handler);
return t;
}};
static final RuntimeException rte = new RuntimeException();
static final Error error = new Error();
static final Throwable weird = new Throwable();
static final Exception checkedException = new Exception();
static class Thrower implements Runnable {
Throwable t;
Thrower(Throwable t) { this.t = t; }
public void run() {
if (t != null)
ThrowingTasks.<RuntimeException>uncheckedThrow(t);
}
}
static final Thrower noThrower = new Thrower(null);
static final Thrower rteThrower = new Thrower(rte);
static final Thrower errorThrower = new Thrower(error);
static final Thrower weirdThrower = new Thrower(weird);
static final Thrower checkedThrower = new Thrower(checkedException);
static final List<Thrower> throwers = Arrays.asList(
noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
static class Flaky implements Runnable {
final Runnable beforeExecute;
final Runnable execute;
Flaky(Runnable beforeExecute,
Runnable execute) {
this.beforeExecute = beforeExecute;
this.execute = execute;
}
public void run() { execute.run(); }
}
static final List<Flaky> flakes = new ArrayList
Other Java examples (source code examples)Here is a short list of links related to this Java ThrowingTasks.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.