static void uncaught(@NonNull Throwable error) { Thread currentThread = Thread.currentThread(); UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler(); handler.uncaughtException(currentThread, error); }
private void reportFailure(final Thread runner, final Throwable thrown) { if (thrown instanceof InterruptedException) { runner.interrupt(); } else { final Thread.UncaughtExceptionHandler ueh = runner.getUncaughtExceptionHandler(); if (ueh != null) { ueh.uncaughtException(runner, thrown); } } }
static void uncaught(@NonNull Throwable error) { Thread currentThread = Thread.currentThread(); UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler(); handler.uncaughtException(currentThread, error); }
public void onFailure(Throwable value) { Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), value); } };
@Override public void accept(Throwable error) throws Exception { Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), error); } });
private void reportException(@NonNull final Throwable throwable) { // Throw uncaught exception that will trigger the report system Thread.currentThread().getUncaughtExceptionHandler() .uncaughtException(Thread.currentThread(), throwable); } });
@Override protected SpringBootExceptionHandler initialValue() { SpringBootExceptionHandler handler = new SpringBootExceptionHandler( Thread.currentThread().getUncaughtExceptionHandler()); Thread.currentThread().setUncaughtExceptionHandler(handler); return handler; }
static void handleError(Throwable ex) { Thread thread = Thread.currentThread(); Throwable t = unwrap(ex); Thread.UncaughtExceptionHandler x = thread.getUncaughtExceptionHandler(); if (x != null) { x.uncaughtException(thread, t); } else { log.error("Scheduler worker failed with an uncaught exception", t); } if (onHandleErrorHook != null) { onHandleErrorHook.accept(thread, t); } }
public void testUncaughtExceptionHandler_custom() { assertEquals( UNCAUGHT_EXCEPTION_HANDLER, builder .setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER) .build() .newThread(monitoredRunnable) .getUncaughtExceptionHandler()); }
private Semaphore terminations = new Semaphore(0); protected void beforeExecute (final Thread thread, final Runnable job) { if (terminations.tryAcquire()) { /* Replace this item in the queue so it may be executed by another * thread */ queue.add(job); thread.setUncaughtExceptionHandler( new ShutdownHandler(thread.getUncaughtExceptionHandler()) ); /* Throwing a runtime exception is the only way to prematurely * cause a worker thread from the TheadPoolExecutor to exit. */ throw new ShutdownException("Terminating thread"); } } public void setCorePoolSize (final int size) { int delta = getActiveCount() - size; super.setCorePoolSize(size); if (delta > 0) { terminations.release(delta); } }
private void handleFatalFailure(Throwable error) { if( failure == null ) { failure = error; mqtt.tracer.debug("Fatal connection failure: %s", error); // Fail incomplete requests. ArrayList<Request> values = new ArrayList(requests.values()); requests.clear(); for (Request value : values) { if( value.cb!= null ) { value.cb.onFailure(failure); } } ArrayList<Request> overflowEntries = new ArrayList<Request>(overflow); overflow.clear(); for (Request entry : overflowEntries) { if( entry.cb !=null ) { entry.cb.onFailure(failure); } } if( listener !=null && !disconnected ) { try { listener.onFailure(failure); } catch (Exception e) { Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e); } } } }
refiller.run(); } catch (Throwable e) { Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
public void testThreadFactoryBuilder_defaults() throws InterruptedException { ThreadFactory threadFactory = builder.build(); Thread thread = threadFactory.newThread(monitoredRunnable); checkThreadPoolName(thread, 1); Thread defaultThread = Executors.defaultThreadFactory().newThread(monitoredRunnable); assertEquals(defaultThread.isDaemon(), thread.isDaemon()); assertEquals(defaultThread.getPriority(), thread.getPriority()); assertSame(defaultThread.getThreadGroup(), thread.getThreadGroup()); assertSame(defaultThread.getUncaughtExceptionHandler(), thread.getUncaughtExceptionHandler()); assertFalse(completed); thread.start(); thread.join(); assertTrue(completed); // Creating a new thread from the same ThreadFactory will have the same // pool ID but a thread ID of 2. Thread thread2 = threadFactory.newThread(monitoredRunnable); checkThreadPoolName(thread2, 2); assertEquals( thread.getName().substring(0, thread.getName().lastIndexOf('-')), thread2.getName().substring(0, thread.getName().lastIndexOf('-'))); // Building again should give us a different pool ID. ThreadFactory threadFactory2 = builder.build(); Thread thread3 = threadFactory2.newThread(monitoredRunnable); checkThreadPoolName(thread3, 1); assertThat(thread2.getName().substring(0, thread.getName().lastIndexOf('-'))) .isNotEqualTo(thread3.getName().substring(0, thread.getName().lastIndexOf('-'))); }
/** * 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); } }
public void testThreadFactory() throws InterruptedException { final String THREAD_NAME = "ludicrous speed"; final int THREAD_PRIORITY = 1; final boolean THREAD_DAEMON = false; ThreadFactory backingThreadFactory = new ThreadFactory() { @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; } }; Thread thread = builder.setThreadFactory(backingThreadFactory).build().newThread(monitoredRunnable); assertEquals(THREAD_NAME, thread.getName()); assertEquals(THREAD_PRIORITY, thread.getPriority()); assertEquals(THREAD_DAEMON, thread.isDaemon()); assertSame(UNCAUGHT_EXCEPTION_HANDLER, thread.getUncaughtExceptionHandler()); assertSame(Thread.State.NEW, thread.getState()); assertFalse(completed); thread.start(); thread.join(); assertTrue(completed); }
/** * Tests whether the exception handler is set if one is provided. */ @Test public void testNewThreadExHandler() { 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(); EasyMock.expect(wrapped.newThread(r)).andReturn(t); EasyMock.replay(wrapped, r, handler); final BasicThreadFactory factory = builder.wrappedFactory(wrapped) .uncaughtExceptionHandler(handler).build(); assertSame("Wrong thread", t, factory.newThread(r)); assertEquals("Wrong exception handler", handler, t .getUncaughtExceptionHandler()); EasyMock.verify(wrapped, r, handler); }
@Test public void verifyThatSetOnThreadSetsTheThreadsHandler() { Thread thread = new Thread(); Implementation handler = new Implementation(null, null); handler.setOnThread(thread); assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler); }
@Test public void verifyCreateDaemonSetExpectedHandler() { UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance(); Thread thread = new LoggingThread("loggingThreadName", null); assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler); }
@Test public void verifyCreateSetExpectedHandler() { UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance(); Thread thread = new LoggingThread("loggingThreadName", false, null); assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler); }
@Test public void verifyThreadHaveExpectedHandler() { UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance(); LoggingThreadFactory factory = new LoggingThreadFactory("baseName"); Thread thread = factory.newThread(null); assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler); }