coder25
coder25

Reputation: 2393

exception handling in executor service

When I throw exception from student name archana. As per my understanding InvokeAll waits for all task to be completed and then return future list

Output I get is

 pool-1-thread-1 Helloprerna   
 pool-1-thread-2 Helloabc   
 HELLO SOMEERROR   
 Execution Completed

I want other tasks output to be show for which exception is not thrown.Any suggestions

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

}

Upvotes: 3

Views: 5997

Answers (2)

Fildor
Fildor

Reputation: 16148

You can move around the try/catch:

Original:

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

will be rather:

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

So here you will get all OK results and you can handle the exceptional execution for each task.

Upvotes: 3

OrdinaryNick
OrdinaryNick

Reputation: 248

The pattern for this should be: Main thread create and call slaves threads, which should return ok value or error value (if there was any error). Then Main thread should collect results from slaves and process them.

Upvotes: 0

Related Questions