public Thread newThread(Runnable r) { Thread t = core.newThread(r); t.setUncaughtExceptionHandler(this); return t; }
/** Sets an UncaughtExceptionHandler for the thread which logs the * Exception stack if the thread dies. */ public static void setLoggingUncaughtExceptionHandler(Thread t) { t.setUncaughtExceptionHandler(LOGGING_EXCEPTION_HANDLER); }
/** * The default uncaught exception handler will print to STDERR, which we don't always want for threads. * Using this utility method you can avoid writing to STDERR on a per-thread basis */ public static void silenceUncaughtExceptionsInThisThread() { Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread ignored, Throwable ignored1) { } }); }
public void start() { if (!running) { thread = new Thread(this::process); thread.setUncaughtExceptionHandler(handler); thread.start(); running = true; } }
@Override public Thread newThread(Runnable runnable) { Thread thread = backingThreadFactory.newThread(runnable); if (nameFormat != null) { thread.setName(format(nameFormat, count.getAndIncrement())); } if (daemon != null) { thread.setDaemon(daemon); } if (priority != null) { thread.setPriority(priority); } if (uncaughtExceptionHandler != null) { thread.setUncaughtExceptionHandler(uncaughtExceptionHandler); } return thread; } };
protected void start() { Assert.notNull(connector, "connector is null"); thread = new Thread(new Runnable() { public void run() { process(); } }); thread.setUncaughtExceptionHandler(handler); running = true; thread.start(); }
public Thread newThread(Runnable r) { Thread t = new Thread(group, r, name + "-" + threadNumber.getAndIncrement(), 0); t.setDaemon(daemon); if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); } t.setUncaughtExceptionHandler(uncaughtExceptionHandler); return t; }
@Override public Thread newThread(Runnable runnable) { Thread t = new Thread(runnable); t.setName(prefix + "-" + COUNTER.incrementAndGet()); t.setDaemon(daemon); if (contextClassLoader != null) { t.setContextClassLoader(contextClassLoader); } if (null != uncaughtExceptionHandler) { t.setUncaughtExceptionHandler(uncaughtExceptionHandler); } return t; }
@Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, name + "-" + index.getAndIncrement(), 0); if (t.isDaemon()) { t.setDaemon(false); } if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); } t.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER); return t; } }
@Override public Thread newThread(Runnable runnable) { Thread thread = backingThreadFactory.newThread(runnable); if (nameFormat != null) { thread.setName(format(nameFormat, count.getAndIncrement())); } if (daemon != null) { thread.setDaemon(daemon); } if (priority != null) { thread.setPriority(priority); } if (uncaughtExceptionHandler != null) { thread.setUncaughtExceptionHandler(uncaughtExceptionHandler); } return thread; } };
private void invokeOnExecutionThreadForTest(Runnable runnable) { executionThread = new Thread(runnable); executionThread.setUncaughtExceptionHandler( new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable e) { thrownByExecutionThread = e; } }); executionThread.start(); }
@Override public void execute(Runnable command) { executionThread = new Thread(command); executionThread.setUncaughtExceptionHandler( new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable e) { thrownByExecutionThread = e; } }); executionThread.start(); } };
@Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName(THREAD_NAME); thread.setPriority(THREAD_PRIORITY); thread.setDaemon(THREAD_DAEMON); thread.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER); return thread; } };
@Override public void start() { if (running) { return; } super.start(); initConnector(); thread = new Thread(new Runnable() { public void run() { process(); } }); thread.setUncaughtExceptionHandler(handler); thread.start(); }
private static List<Throwable> runThreadsAndReturnExceptions(int numThreads, Runnable task) { List<Thread> threads = new ArrayList<>(numThreads); final List<Throwable> exceptions = new ArrayList<>(numThreads); for (int i = 0; i < numThreads; i++) { Thread thread = new Thread(task); thread.setUncaughtExceptionHandler( new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { exceptions.add(e); } }); threads.add(thread); } for (Thread t : threads) { t.start(); } for (Thread t : threads) { Uninterruptibles.joinUninterruptibly(t); } return exceptions; }
@Test public void onErrorNoHandler() { try { final List<Throwable> list = new ArrayList<Throwable>(); RxJavaPlugins.setErrorHandler(null); Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { list.add(e); } }); RxJavaPlugins.onError(new TestException("Forced failure")); Thread.currentThread().setUncaughtExceptionHandler(null); // this will be printed on the console and should not crash RxJavaPlugins.onError(new TestException("Forced failure 3")); assertEquals(1, list.size()); assertUndeliverableTestException(list, 0, "Forced failure"); } finally { RxJavaPlugins.reset(); Thread.currentThread().setUncaughtExceptionHandler(null); } }
@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); } }
/** * Tests whether the original exception handler is not touched if none is * specified. */ @Test public void testNewThreadNoExHandler() { final ThreadFactory wrapped = EasyMock.createMock(ThreadFactory.class); final Runnable r = EasyMock.createMock(Runnable.class); final Thread.UncaughtExceptionHandler handler = EasyMock .createMock(Thread.UncaughtExceptionHandler.class); final Thread t = new Thread(); t.setUncaughtExceptionHandler(handler); EasyMock.expect(wrapped.newThread(r)).andReturn(t); EasyMock.replay(wrapped, r, handler); final BasicThreadFactory factory = builder.wrappedFactory(wrapped).build(); assertSame("Wrong thread", t, factory.newThread(r)); assertEquals("Wrong exception handler", handler, t .getUncaughtExceptionHandler()); EasyMock.verify(wrapped, r, handler); } }
@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); } }
@Test public void pluginCrash() { Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { throw new TestException("Second"); } }); CompositeDisposable set = new CompositeDisposable(); final ScheduledRunnable run = new ScheduledRunnable(new Runnable() { @Override public void run() { throw new TestException("First"); } }, set); set.add(run); try { run.run(); fail("Should have thrown!"); } catch (TestException ex) { assertEquals("Second", ex.getMessage()); } finally { Thread.currentThread().setUncaughtExceptionHandler(null); } assertTrue(run.isDisposed()); assertEquals(0, set.size()); }