aspose file tools
The moose likes Threads and Synchronization and the fly likes Future task is done or not ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Future task is done or not ?" Watch "Future task is done or not ?" New topic
Author

Future task is done or not ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 758
In the Future, does the main thread need to wait for the future job ?

for example

in the Line A, why we not need to make sure if the thread job is done ? something like



Then next question is , how could we implement this: if p, q thread is not done in 10s, main thread will throw exception and terminate the logic.

Thanks
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
The get() method in Future is defined to block until the task has completed, or until an exception is thrown. So this is already taken care of for you. There's no need to implement waiting.
[ January 20, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
As Jim stated, get() method will block the current thread till the task is done. You could call the overloaded method get(10,TimeUnit.SECONDS) which would block about 10 seconds then throw Timeout Exception if it the task has not been completed.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Future task is done or not ?
 
Similar Threads
Reverse of Long.toHexString()
does local variables which are object and final are still stored in stack?
Handling timeout using thread
Concurrent Modification Exception ?
Anonymous Inner Classes