aspose file tools*
The moose likes Threads and Synchronization and the fly likes java.util.concurrent.Future with callable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "java.util.concurrent.Future with callable" Watch "java.util.concurrent.Future with callable" New topic
Author

java.util.concurrent.Future with callable

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 556
I wrote the following prog to try out callable an future


output is :

pool-1-thread-18 took 110
pool-1-thread-6 took 110
pool-1-thread-16 took 120
pool-1-thread-9 took 135
pool-1-thread-8 took 138
pool-1-thread-4 took 141
pool-1-thread-7 took 175
pool-1-thread-15 took 184
pool-1-thread-10 took 198
pool-1-thread-17 took 204
pool-1-thread-12 took 226
pool-1-thread-2 took 229
pool-1-thread-3 took 234
pool-1-thread-11 took 236
pool-1-thread-1 took 238
pool-1-thread-5 took 239
pool-1-thread-19 took 242
pool-1-thread-20 took 244

Starts processing the futures:
result is pool-1-thread-19 took 242
result is pool-1-thread-8 took 138
result is pool-1-thread-20 took 244
result is pool-1-thread-7 took 175
result is pool-1-thread-9 took 135
result is pool-1-thread-12 took 226
result is pool-1-thread-11 took 236
result is pool-1-thread-15 took 184
result is pool-1-thread-2 took 229
result is pool-1-thread-10 took 198
result is pool-1-thread-3 took 234
result is pool-1-thread-1 took 238
result is pool-1-thread-16 took 120
result is pool-1-thread-4 took 141
result is pool-1-thread-17 took 204
result is pool-1-thread-6 took 110
result is pool-1-thread-5 took 239
result is pool-1-thread-18 took 110

Still processing callables
pool-1-thread-13 took 252
pool-1-thread-14 took 252

The pool thread is the callable worker processing, so as you can see my future ran before all the callables had finished, I can set a time on my get but in the real world will you know how long it will take to process all your callables.

I cant see how this problem is addressed any idea

thanks for any help
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Have you captured all the Futures of all the tasks spawned? From the code it looks like you have captured the Future of the last task spawned. Though all the Callables are updating the same ConcurrentHashMap, there might be a case where one of the callables hasn't completed, like the case you have pointed. I think one would fetch the result from the Future corresponding to that Callable.

You can try to capture all the Future instances into a List i.e add the result from executor.submit(worker) into a List and then iterate through the list so that you are sure that each of the Callable has completed execution. Depending on how your worker is implemented, it might return the same concurrentThreadTimings map with each task/callable updating its key-value entry OR you can even return one value for each Callable/Task and then do a Future.get() to fetch the return value for each task and store it in a Map.


Mohamed Sanaulla | My Blog
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 556
Hi Mohamed thanks for the reply the second option is the best one each callable returning a its data an building up the map at the end.

This was more of a sort of a proof of concept an understand how futures an callables work together,

It looks that although get does wait, for a callable to finish, if you are updating one future with multiple callables it does not know that or wait for all the callables to uodate that none future.
Well understanding that concept means i wont make that mistake for real in misunderstanding futures.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Tony Evans wrote:Hi Mohamed thanks for the reply the second option is the best one each callable returning a its data an building up the map at the end.

This was more of a sort of a proof of concept an understand how futures an callables work together,

It looks that although get does wait, for a callable to finish, if you are updating one future with multiple callables it does not know that or wait for all the callables to uodate that none future.
Well understanding that concept means i wont make that mistake for real in misunderstanding futures.

Even I have been going through these concepts using the Java Concurrency in Practice book. I am yet to reach the ExecutorService chapter. I would also be writing such code in the coming days
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 556
Hi Mohamed,

wrote another proof of concept


And yes its one future a callable, you cant have multiple callables to one future, as I thought may have been possible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.concurrent.Future with callable
 
Similar Threads
Thread problem
jtable would like to handle both single click and double click
zip / pipedinputstream / pipedoutputstream problem
ExecutorService and FutureTask - Blocked Thread
Issue with executor thread pool