java.lang.Thread.getUncaughtExceptionHandler()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中java.lang.Thread.getUncaughtExceptionHandler()方法的一些代码示例,展示了Thread.getUncaughtExceptionHandler()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.getUncaughtExceptionHandler()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:getUncaughtExceptionHandler

Thread.getUncaughtExceptionHandler介绍

[英]Returns the handler invoked when this thread abruptly terminates due to an uncaught exception. If this thread has not had an uncaught exception handler explicitly set then this thread's ThreadGroup object is returned, unless this thread has terminated, in which case null is returned.
[中]返回由于未捕获异常导致此线程突然终止时调用的处理程序。如果此线程未显式设置未捕获异常处理程序,则返回此线程的ThreadGroup对象,除非此线程已终止,在这种情况下返回null。

代码示例

代码示例来源:origin: ReactiveX/RxJava

static void uncaught(@NonNull Throwable error) {
  Thread currentThread = Thread.currentThread();
  UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
  handler.uncaughtException(currentThread, error);
}

代码示例来源:origin: graphql-java/graphql-java

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);
    }
  }
}

代码示例来源:origin: redisson/redisson

static void uncaught(@NonNull Throwable error) {
  Thread currentThread = Thread.currentThread();
  UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
  handler.uncaughtException(currentThread, error);
}

代码示例来源:origin: fusesource/mqtt-client

public void onFailure(Throwable value) {
    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), value);
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void accept(Throwable error) throws Exception {
    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), error);
  }
});

代码示例来源:origin: TeamNewPipe/NewPipe

private void reportException(@NonNull final Throwable throwable) {
    // Throw uncaught exception that will trigger the report system
    Thread.currentThread().getUncaughtExceptionHandler()
        .uncaughtException(Thread.currentThread(), throwable);
  }
});

代码示例来源:origin: org.springframework.boot/spring-boot

@Override
protected SpringBootExceptionHandler initialValue() {
  SpringBootExceptionHandler handler = new SpringBootExceptionHandler(
      Thread.currentThread().getUncaughtExceptionHandler());
  Thread.currentThread().setUncaughtExceptionHandler(handler);
  return handler;
}

代码示例来源:origin: reactor/reactor-core

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);
  }
}

代码示例来源:origin: google/guava

public void testUncaughtExceptionHandler_custom() {
 assertEquals(
   UNCAUGHT_EXCEPTION_HANDLER,
   builder
     .setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER)
     .build()
     .newThread(monitoredRunnable)
     .getUncaughtExceptionHandler());
}

代码示例来源:origin: stackoverflow.com

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);
  }
}

代码示例来源:origin: fusesource/mqtt-client

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);
      }
    }
  }
}

代码示例来源:origin: fusesource/mqtt-client

refiller.run();
} catch (Throwable e) {
  Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);

代码示例来源:origin: google/guava

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('-')));
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
   * 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);
  }
}

代码示例来源:origin: google/guava

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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * 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);
}

代码示例来源:origin: apache/geode

@Test
public void verifyThatSetOnThreadSetsTheThreadsHandler() {
 Thread thread = new Thread();
 Implementation handler = new Implementation(null, null);
 handler.setOnThread(thread);
 assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler);
}

代码示例来源:origin: apache/geode

@Test
public void verifyCreateDaemonSetExpectedHandler() {
 UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance();
 Thread thread = new LoggingThread("loggingThreadName", null);
 assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler);
}

代码示例来源:origin: apache/geode

@Test
public void verifyCreateSetExpectedHandler() {
 UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance();
 Thread thread = new LoggingThread("loggingThreadName", false, null);
 assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler);
}

代码示例来源:origin: apache/geode

@Test
public void verifyThreadHaveExpectedHandler() {
 UncaughtExceptionHandler handler = LoggingUncaughtExceptionHandler.getInstance();
 LoggingThreadFactory factory = new LoggingThreadFactory("baseName");
 Thread thread = factory.newThread(null);
 assertThat(thread.getUncaughtExceptionHandler()).isSameAs(handler);
}

相关文章