Jay Roy
asked on
ExecutorService in laymans terms
hi guys
From java 1.5 API definition of ExecutorService is :
An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.
I am new to java concurreny API.Can anyone in very simple layman language explain what this means with a very simple example?
thanks
From java 1.5 API definition of ExecutorService is :
An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.
I am new to java concurreny API.Can anyone in very simple layman language explain what this means with a very simple example?
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
also in what situations do i use callable v/s runnable
executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception { return doTaskWork(); }}) ;
v/s
executor.submit(Runnable task)
thx
executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception { return doTaskWork(); }}) ;
v/s
executor.submit(Runnable task)
thx
The simplest form is you just submit a Runnable task. In that case it's "fire and forget". You submit the task and it will run later. This is fine if you just want to start a thread to do some work, don't really care when it runs or are interested in getting the result of the run. (A typical example might be to handle a http request in a web server. You just need the work done and the result will go back to the sender of the request).
If you submit a callable, the Future object allows you to examine how it will behave in the future:
Future<Boolean> future = executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception { return doTaskWork(); }}) ;
This gives you more control and insight into when this task runs. You can now call:
future.isDone() ;
to find out if it's finished yet.
You can also call:
Boolean result = future.get() ;
to block and wait for the task to finish and get the result of the method. (It's a boolean here because I defined Callback<Boolean>. You can return anything you want). What's really nice is this also will throw an exception if the underlying method threw an exception. This is often a problem with threads - if they throw an exception it's hard to signal that to the person who started the thread. This lets you solve that).
Hope that helps,
Doug
If you submit a callable, the Future object allows you to examine how it will behave in the future:
Future<Boolean> future = executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception { return doTaskWork(); }}) ;
This gives you more control and insight into when this task runs. You can now call:
future.isDone() ;
to find out if it's finished yet.
You can also call:
Boolean result = future.get() ;
to block and wait for the task to finish and get the result of the method. (It's a boolean here because I defined Callback<Boolean>. You can return anything you want). What's really nice is this also will throw an exception if the underlying method threw an exception. This is often a problem with threads - if they throw an exception it's hard to signal that to the person who started the thread. This lets you solve that).
Hope that helps,
Doug
ASKER
>>>Future<Boolean> future = executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception { return doTaskWork(); }}) ;
This gives you more control and insight into when this task runs. You can now call:
future.isDone() ;
Cant i do the above using a Runnable task? what i am trying to understand is
submit(Callable<T> task) returns a Future
submit(Runnable task) returns a Future
so how are they different?
can you show me with a simple example?
thanks
public Boolean call() throws Exception { return doTaskWork(); }}) ;
This gives you more control and insight into when this task runs. You can now call:
future.isDone() ;
Cant i do the above using a Runnable task? what i am trying to understand is
submit(Callable<T> task) returns a Future
submit(Runnable task) returns a Future
so how are they different?
can you show me with a simple example?
thanks
Callable returns something, Runnable doesn't
Callable was a later addition
http://geekexplains.blogspot.com/2008/05/callable-interface-vs-runnable.html
Callable was a later addition
http://geekexplains.blogspot.com/2008/05/callable-interface-vs-runnable.html
If you don't care what the result of running the task is you can use Runnable. This is like a simple thread.
If you want the result of the task you should use Callable.
If you want the result of the task you should use Callable.
ASKER
public Boolean call() throws Exception { return doTaskWork(); }}) ;
submit takes Callable paramter and returns Future . not sure what these mean.
can you give a simple example.
thx