Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

waiting for an executor service task completion  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a requirement, I have a task object, it will run for some undeterministic time and when the task is over, I need to return the result to a map. now the task is implementing runnable. the code was working fine with threads, but I am supposed to update it with executor service and improve the current solution. I have created a sample code scenario, I need to find the right approach to wait for the task to complete. here is the code:




Here one way I see is to use awaitTermination(), which I am not sure how much time I should put so as to not create unnecessary processing overhead. other way is to use the future.get() method, it works, but doesnt look elegant. I am not sure how my seniors will react to its use like that.

please help me in improving the solution which looks simpler and effective.

Thanks
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the future.get() method, it works, but doesnt look elegant.

What's inelegant with declaring a callable and using the get method? Why do you think it is not elegant?
 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Kumarr wrote:
the future.get() method, it works, but doesnt look elegant.

What's inelegant with declaring a callable and using the get method? Why do you think it is not elegant?

well, I am not sending any object, just changing the state of that object, so callable is not a requirement. I said it doesnt look elegant because it looks too crude to be used if we are not waiting for any value.

I thought of trying something like

but which method looks more appropriate is what I wanted to know... awaitTermination or get.. or if there is some other way to handle it more gracefully, that would be more helpful.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Future.isDone() - can't that method be used?
 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Kumarr wrote:Future.isDone() - can't that method be used?


but isDone() will not wait for task completion right? I do not want the next statement to run as the result will only be available when this runnable executes.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to wait till the execution completes, then probably you have to use "get" which will wait till result is available. It will return the
result whenever available. Result can be an exception, error or some value.

 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:If you want to wait till the execution completes, then probably you have to use "get" which will wait till result is available. It will return the
result whenever available. Result can be an exception, error or some value.



Well, that is what I initially used. But as I am using runnable, it doesn't look proper. for the time being I am using awaitTermination() and future.isDone() conditions..
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand what you think non-elegant here. You can also use overloaded method of get, which waits for a particular time. You
can check here.
 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:I still don't understand what you think non-elegant here. You can also use overloaded method of get, which waits for a particular time. You
can check here.


well, the reason is that the task is run from a list, each task might take some time. the other way also does similar thing. for a single task, get would be better i guess..
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have multiple task to complete and if you don't care of the order in which they complete. Then you can use Completion service as well.
This is worked as a queue. Whatever task is completed it will return you the result of that task. You can check here.
 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:If you have multiple task to complete and if you don't care of the order in which they complete. Then you can use Completion service as well.
This is worked as a queue. Whatever task is completed it will return you the result of that task. You can check here.


Yes, I saw that. Will think of adding it, once I complete my other issues.
 
s ravi chandran
Ranch Hand
Posts: 549
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think completion service will not be applicable in my case. I am using single thread executor to run the task at a different place. the place where I have all the task is different component, and there I do not have a runnable or callable class.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:
I have a requirement, I have a task object, it will run for some undeterministic time and when the task is over, I need to return the result to a map. now the task is implementing runnable. the code was working fine with threads, but I am supposed to update it with executor service and improve the current solution.

Here one way I see is to use awaitTermination(), which I am not sure how much time I should put so as to not create unnecessary processing overhead. other way is to use the future.get() method, it works, but doesnt look elegant. I am not sure how my seniors will react to its use like that.


Your code creates an executor (which creates threads, work queues, and lots of internal structures), and a task. Stores the executor in a local reference variable, submits one task, and then terminates the executor, awaiting for completion. Upon termination, this has to get rid of the threads, work queues, etc. etc. etc.

How is this much different than just starting a thread to execute the task? Isn't this a lot of complexity for doing the exact same thing, and no added benefit? One of benefits of executors (thread pools) is reuse, meaning the threads are not created and torn down immediately. This seems like doing the same thing, only with different threading classes.

Henry
 
raja undru
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use invokeAll method, which blocks until all tasks are finished. But you need to pass collection of callable task. So convert them using Executors.callable().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!