[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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
0
royjayd
Asked:
royjayd
  • 3
  • 3
  • 2
2 Solutions
 
objectsCommented:
An Executor is something that can executed some time in the furture. Its used to seperating submitting a job from how it actually be run.

ExecutorService provides an Executor implementation that manages the execution amny asynchronous tasks. You submit task for it to execute and it handles the actual running of them for you.

http://www.vogella.de/articles/JavaConcurrency/article.html#threadpools
0
 
dpearsonCommented:
If you want to run a series of tasks in parallel the way you used to do it was to start one thread for each task, have them run until they completed each parallel task and then stop.

The problem with this approach is that if you have 100 tasks you would start 100 threads, but that's not really the most efficient approach.  If you have a machine with 2 Quad Core CPUs you want about 10 threads running at once.  So how to divide that 100 tasks among 10 threads?

That problem is solved by the Executor framework.  It allows you to easily create an "Executor" - which most often is a pool of threads.  For my example, you'd create a pool with 10 threads.  Then you submit tasks to the Executor and it handles the issue of how best to allocate them to the 10 threads, watching when a thread has finished doing work and submitting a new task to it.

In its simplest form the code looks something like this:

// I'd like a pool of 10 threads
ExecutorService executor = Executors.newFixedThreadPool(10)

// Give it a task to start working on
executor.      submit(new Callable<Boolean>() {
              public Boolean call() throws Exception { return doTaskWork(); }}) ;

// This is your real task
public boolean doTaskWork() {
}

Doug
0
 
royjaydAuthor Commented:
>>>executor.submit(new Callable<Boolean>() {
              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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
royjaydAuthor Commented:
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
0
 
dpearsonCommented:
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
0
 
royjaydAuthor Commented:
>>>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
0
 
objectsCommented:
Callable returns something, Runnable doesn't
Callable was a later addition

http://geekexplains.blogspot.com/2008/05/callable-interface-vs-runnable.html
0
 
dpearsonCommented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now