Java 通知调用方法线程中发生的异常
我正在编写一个SpringMVC应用程序 我正在使用Java 通知调用方法线程中发生的异常,java,multithreading,spring-mvc,exception-handling,Java,Multithreading,Spring Mvc,Exception Handling,我正在编写一个SpringMVC应用程序 我正在使用ThreadPoolTaskExecutor执行任务 我有下面的示例代码 MyClass.java public class MyClass { public void startProcess() { ThreadPoolTaskExecutor taskExecutor = //Initializing for (int i = 1; i <= 5; i++) { ta
ThreadPoolTaskExecutor
执行任务
我有下面的示例代码
MyClass.java
public class MyClass {
public void startProcess() {
ThreadPoolTaskExecutor taskExecutor = //Initializing
for (int i = 1; i <= 5; i++) {
taskExecutor.execute(new MyRunnable());
// I can call taskExecutor.submit(task); also, if required
}
}
}
public class MyRunnable implements Runnable {
@Override
public void onRun() {
try {
//Code which generates exception like below
throw new Exception("Runtime Exception");
} catch (Exception e1) {
// log or throw the exception
}
}
}
我想通知startProcess()
有关MyRunnable的run方法中发生的异常
谁能给我指点一下吗
我发现下面的链接,但它并不能解决我的问题
回答我从得到的@Async问题,你可以在你的线程中添加带有监听器的构造函数 比如: 例外情况监听器:
public class ExceptionListener{
public void doSomething(long threadId,Exception e){
//...e.g., notify startProcess()
}
}
MyRunnable:
public class MyRunnable implements Runnable {
private ExceptionListener listener;
private MyRunnable(ExceptionListener exception) {
this.listener = listener;
}
@Override
public void run() {
//...
listener.doSomething(Thread.currentThread().getId(),new Exception("Runtime Exception"));
//...
}
}
startProcess():
public void startProcess(){
ThreadPoolTaskExecutor taskExecutor=//正在初始化
ExceptionListener listener=新的ExceptionListener();
对于(int i=1;i而不是Runnable
,实现Callable
。Callable
可以引发异常,当您使用Future
检索可调用的结果时,您将得到作为ExecutionException
引发的异常:
public class MyCallable implements Callable<Void> {
public Void call() throws Exception {
try {
//Code which generates exception like below
throw new Exception("Runtime Exception");
} catch (Exception e1) {
// log or throw the exception
}
return null; // To satisfy the method signature
}
}
公共类MyCallable实现可调用{
public Void call()引发异常{
试一试{
//生成如下异常的代码
抛出新异常(“运行时异常”);
}捕获(异常e1){
//记录或抛出异常
}
返回null;//以满足方法签名
}
}
在MyClass中:
List<Future<Void>> futures = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
Future<Void> future = taskExecutor.submit(new MyCallable());
futures.add(future);
}
// After all tasks have started, now wait for all of them to complete (they run in parallel)
// and check if there were any exceptions
for (Future<Void> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
// Access the exception thrown by the different thread.
e.getCause().printStackTrace();
}
}
List futures=new ArrayList();
对于(int i=1;我的意思是,在调用execute方法后,我应该检查startProcess
中的listener
?在UncaughtExceptionHandler示例中,我应该将Thread.UncaughtExceptionHandler的单个对象传递给每个线程,还是应该为每个线程创建单个对象?实际上,在大多数情况下,应该在相同的线程。您想要实现什么?我应该将thread.UncaughtExceptionHandler的单个对象传递给每个线程,还是应该为每个线程创建单个对象?-->您在启动它之前将其设置为thread对象,就像t.setUncaughtExceptionHandler(thread.UncaughtExceptionHandler);因此,这取决于您是否对所有进程使用一个或对每个进程使用不同的方法…如果发生异常,我必须停止对其他进程的进一步执行。因此,我希望在startProcess方法中捕获异常或获得有关异常的通知。我是否需要检查isDone()条件?否,get
方法(无超时)将一直等到它完成。除非您的主线程被中断,但在这种情况下,您将得到一个InterruptedException
。还有一个问题。我不知道它是否与此相关。如果我使用@Async作为对其他方法的异步调用,并且如果我想检查异步方法中发生的异常,那么该怎么办我应该这样做吗?因为async方法还返回future对象。我在这里找到了@async question()的答案。
List<Future<Void>> futures = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
Future<Void> future = taskExecutor.submit(new MyCallable());
futures.add(future);
}
// After all tasks have started, now wait for all of them to complete (they run in parallel)
// and check if there were any exceptions
for (Future<Void> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
// Access the exception thrown by the different thread.
e.getCause().printStackTrace();
}
}