File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes ExecutorCompletionService Submit(Runnable,int) to return different int values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ExecutorCompletionService Submit(Runnable,int) to return different int values" Watch "ExecutorCompletionService Submit(Runnable,int) to return different int values" New topic
Author

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

Bob Mathews
Ranch Hand

Joined: Feb 26, 2006
Posts: 43
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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
If you cannot change the object to Callable, then decorate it with Callable:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExecutorCompletionService Submit(Runnable,int) to return different int values