This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt regarding the ExecutorService future object  RSS feed

 
Aniket Harode
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have these code



the op of the following prog. is like that
start
end
futurefalse
futurefalse
shutdown
get the result
get the result0
get the result1
get the result2


But  i don't understand  why the future.isDone is printing the false as the future object is generating
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aniket Harode wrote:
But  i don't understand  why the future.isDone is printing the false as the future object is generating


The isDone() method is returning false, because the task that the future object is representing has not completed yet. Not exactly sure of your confusion though.

Henry
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try using Future.get() to check if the task got completed ?
it is a blocking call and may block until the task gets completed.

Post that invoking future.isDone() may return TRUE. I guess


You can try calling get(..) with an extremely short timeout. If it returns a value, it was completed.
If you get a TimeoutException, it wasn't.
If you get any other of the possible exceptions, it was either cancelled, failed, or was interrupted

When you call Future.get() method, there is 4 possible outcomes:
You get the result value
You get CancellationException - if the computation was cancelled (e.g. Future.cancel(true) is called)
You get ExecutionException - if the computation threw an exception
You get InterruptedException - if the current thread was interrupted while waiting (e.g. executor.shutdownNow() is called)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!