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

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

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

Thread.setUncaughtExceptionHandler介绍

[英]Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

A thread can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set. If no such handler is set then the thread's ThreadGroup object acts as its handler.
[中]设置当此线程由于未捕获异常而突然终止时调用的处理程序。
通过显式设置未捕获异常处理程序,线程可以完全控制其响应未捕获异常的方式。如果未设置此类处理程序,则线程的ThreadGroup对象将充当其处理程序。

代码示例

代码示例来源:origin: jenkinsci/jenkins

public Thread newThread(Runnable r) {
  Thread t = core.newThread(r);
  t.setUncaughtExceptionHandler(this);
  return t;
}

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

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

代码示例来源:origin: Graylog2/graylog2-server

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

代码示例来源:origin: alibaba/canal

public void start() {
  if (!running) {
    thread = new Thread(this::process);
    thread.setUncaughtExceptionHandler(handler);
    thread.start();
    running = true;
  }
}

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

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

代码示例来源:origin: alibaba/canal

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

代码示例来源:origin: alibaba/canal

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

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

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

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

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

代码示例来源:origin: prestodb/presto

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

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

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

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

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

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

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

代码示例来源:origin: alibaba/canal

@Override
public void start() {
  if (running) {
    return;
  }
  super.start();
  initConnector();
  thread = new Thread(new Runnable() {
    public void run() {
      process();
    }
  });
  thread.setUncaughtExceptionHandler(handler);
  thread.start();
}

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

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

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

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

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

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

代码示例来源: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: ReactiveX/RxJava

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

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

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

相关文章