• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExecutorCompletionService Submit(Runnable,int) to return different int values

 
Bob Mathews
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have an ExecutorCompletionService.

and I call the submit with a Runnable object (and not Callable)

completionService.submit(runnable, 1);

Now when the task completes I get a Future object and when I call get(), I always get 1 as the output as I have specified it while calling the Submit.

My requirement is that I want to return some other int like -1 when something wrong happens while executing the task.

How can I do this?

I cannot change the Object from Runnable to Callable.


Thanks
Bob
 
Henry Wong
author
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when the task completes I get a Future object and when I call get(), I always get 1 as the output as I have specified it while calling the Submit.

My requirement is that I want to return some other int like -1 when something wrong happens while executing the task.

How can I do this?


What do you mean by "something wrong happens"? A runnable throws no exceptions and returns no values... how do you expect the executor to detect this "something wrong happens"?

Anyway, think of the value returned as a token. It could be an array index, it could be a key to a hashmap, etc. You should use this token to get the result, so if "something wrong happens", you should store the result there. The token is not the result, but a way to get the result.

Henry
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cannot change the object to Callable, then decorate it with Callable:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic