执行者服务中的异常处理
exception handling in executor service
当我从学生姓名 archana 抛出异常时。
根据我的理解 InvokeAll 等待所有任务完成然后 return 未来列表
我得到的输出是
pool-1-thread-1 Helloprerna
pool-1-thread-2 Helloabc
HELLO SOMEERROR
Execution Completed
我希望显示其他任务输出,其中异常不是 thrown.Any 建议
public class Executor {
public static void main(String args[]) throws InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(5);
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("prerna"));
list.add(new Student("abc"));
list.add(new Student("archana"));
list.add(new Student("def"));
list.add(new Student("xyz"));
list.add(new Student("ritu"));
list.add(new Student("babita"));
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
//if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
//e.printStackTrace();
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
System.out.println("Execution Completed");
}
}
.
public class Student implements Callable<String>{
String name;
public Student(String name) {
super();
this.name = name;
}
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
if(name=="archana"){
throw new Exception();
}
return display(name);
}
private String display(String name2) {
try {
// System.out.println(Thread.currentThread().getName());
name2=Thread.currentThread().getName()+" Hello"+ name;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name2;
}
}
您可以在 try/catch 周围移动:
原文:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
// if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
// e.printStackTrace();
}
将是:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
try{
System.out.println(f.get());
}catch (ExecutionException e) {
System.out.println("HELLO SOME ERROR: " + e.getMessage());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
所以在这里你会得到所有好的结果,你可以处理每个任务的异常执行。
这个模式应该是:主线程创建并调用从属线程,它应该 return ok 值或错误值(如果有任何错误)。然后主线程应该从奴隶那里收集结果并处理它们。
当我从学生姓名 archana 抛出异常时。 根据我的理解 InvokeAll 等待所有任务完成然后 return 未来列表
我得到的输出是
pool-1-thread-1 Helloprerna
pool-1-thread-2 Helloabc
HELLO SOMEERROR
Execution Completed
我希望显示其他任务输出,其中异常不是 thrown.Any 建议
public class Executor {
public static void main(String args[]) throws InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(5);
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("prerna"));
list.add(new Student("abc"));
list.add(new Student("archana"));
list.add(new Student("def"));
list.add(new Student("xyz"));
list.add(new Student("ritu"));
list.add(new Student("babita"));
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
//if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
//e.printStackTrace();
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
System.out.println("Execution Completed");
}
}
.
public class Student implements Callable<String>{
String name;
public Student(String name) {
super();
this.name = name;
}
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
if(name=="archana"){
throw new Exception();
}
return display(name);
}
private String display(String name2) {
try {
// System.out.println(Thread.currentThread().getName());
name2=Thread.currentThread().getName()+" Hello"+ name;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name2;
}
}
您可以在 try/catch 周围移动:
原文:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
// if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
// e.printStackTrace();
}
将是:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
try{
System.out.println(f.get());
}catch (ExecutionException e) {
System.out.println("HELLO SOME ERROR: " + e.getMessage());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
所以在这里你会得到所有好的结果,你可以处理每个任务的异常执行。
这个模式应该是:主线程创建并调用从属线程,它应该 return ok 值或错误值(如果有任何错误)。然后主线程应该从奴隶那里收集结果并处理它们。