use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.
the class RxJavaPluginsTest method onErrorCrashes.
@Test
public void onErrorCrashes() {
try {
final List<Throwable> list = new ArrayList<Throwable>();
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
throw new TestException("Forced failure 2");
}
});
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
list.add(e);
}
});
RxJavaPlugins.onError(new TestException("Forced failure"));
assertEquals(2, list.size());
assertTestException(list, 0, "Forced failure 2");
assertUndeliverableTestException(list, 1, "Forced failure");
Thread.currentThread().setUncaughtExceptionHandler(null);
} finally {
RxJavaPlugins.reset();
Thread.currentThread().setUncaughtExceptionHandler(null);
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.
the class RxJavaPluginsTest method onErrorWithNull.
@Test
public void onErrorWithNull() {
try {
final List<Throwable> list = new ArrayList<Throwable>();
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
throw new TestException("Forced failure 2");
}
});
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
list.add(e);
}
});
RxJavaPlugins.onError(null);
assertEquals(2, list.size());
assertTestException(list, 0, "Forced failure 2");
assertNPE(list, 1);
RxJavaPlugins.reset();
RxJavaPlugins.onError(null);
assertNPE(list, 2);
} finally {
RxJavaPlugins.reset();
Thread.currentThread().setUncaughtExceptionHandler(null);
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project OpenGrok by OpenGrok.
the class Executor method registerErrorHandler.
public static void registerErrorHandler() {
UncaughtExceptionHandler dueh = Thread.currentThread().getDefaultUncaughtExceptionHandler();
if (dueh == null) {
LOGGER.log(Level.FINE, "Installing default uncaught exception handler");
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOGGER.log(Level.SEVERE, "Uncaught exception in thread " + t.getName() + " with ID " + t.getId() + ": " + e.getMessage(), e);
}
});
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project jersey by jersey.
the class ThreadFactoryBuilder method build.
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final String nameFormat = builder.nameFormat;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
final ThreadFactory backingThreadFactory = (builder.backingThreadFactory != null) ? builder.backingThreadFactory : Executors.defaultThreadFactory();
final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
if (nameFormat != null) {
thread.setName(String.format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}
use of java.lang.Thread.UncaughtExceptionHandler in project randomizedtesting by randomizedtesting.
the class LocalSlaveStreamHandler method start.
@Override
public void start() throws IOException {
lastActivity = System.currentTimeMillis();
pumpers.add(new Thread(new StreamPumper(stdout, sysout), "pumper-stdout"));
pumpers.add(new Thread(new StreamPumper(stderr, syserr), "pumper-stderr"));
pumpers.add(new Thread("pumper-events") {
public void run() {
pumpEvents(eventStream);
}
});
if (heartbeat > 0) {
pumpers.add(watchdog = new Thread("pumper-watchdog") {
public void run() {
final long heartbeatMillis = TimeUnit.SECONDS.toMillis(heartbeat);
final long HEARTBEAT = Math.max(500, heartbeatMillis / 5);
final long HEARTBEAT_EVENT_THRESHOLD = heartbeatMillis;
try {
long lastObservedUpdate = lastActivity;
long reportDeadline = lastObservedUpdate + HEARTBEAT_EVENT_THRESHOLD;
while (true) {
Thread.sleep(HEARTBEAT);
Long last = lastActivity;
if (last == null) {
// terminated.
break;
}
if (last != lastObservedUpdate) {
lastObservedUpdate = last;
reportDeadline = last + HEARTBEAT_EVENT_THRESHOLD;
} else {
final long current = System.currentTimeMillis();
if (current >= reportDeadline) {
eventBus.post(new LowLevelHeartBeatEvent(last, current));
reportDeadline = System.currentTimeMillis() + HEARTBEAT_EVENT_THRESHOLD;
}
}
}
} catch (InterruptedException e) {
// terminate on interrupt.
}
}
});
}
// Start all pumper threads.
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
warnStream.println("Unhandled exception in thread: " + t);
e.printStackTrace(warnStream);
}
};
for (Thread t : pumpers) {
t.setUncaughtExceptionHandler(handler);
t.setDaemon(true);
t.start();
}
}
Aggregations