Saturday, July 12, 2014
Today I am going to deal with an e3xcellent feature of the Java Concurrency framework. You can consider it as one of the most basic things of the framework. Here I will show you how to use the Executor with a simple demo example. You would like to know why to use Executor? Earlier you must have learnt during your basic learning on Java threads, that whenever you will have to do multi-threading you will have to create a task either implementing Runnable interface or extending Thread class. Then you would have to start the thread created by calling start() method. This is actually very low-level implementation where you are dealing with threads. This is completely alright for small applications. But when you are handling large applications, you would never like to handle and manage threads directly, as your main work is to deal with the task properly and concentrate on that. So keeping that in mind, her comes the advantage of Executor where you only have to create the task and
submit it to the executor. It will automatically create a new thread and assign the task to it. Its advantage is not much noticable in small cases. But suppose you have a large number of tasks and your system supports only a small number of threads to be run efficiently. In that case, if you manually create threads and assign tasks and manage them throttling resources will be a very difficult task. In those cases, all thnose things will be managed without much of programmer's effort as the tasks will be kept in queue and will re-use the worker threads, as creating new threads consumes a lot of resources. To sum up, it completely depends on Executor implementation that whether a new thread, or in a pooled thread or in the current thread the task will be executed. More of this and resource throttling will be discussed with proper examples in later tutorials.
       Here in the example below we will just create a private inner class that implements Runnable interface and try to print odd numbers. To understand multi-threading we will carry  out another task of printing even numbers in the main thread. We will submit the task of odd number generation to a executor and execute it; The executor for the task is created by calling newSingleThreadExecutor() meaning that a executor with only one thread is created which will carry out the task.
------------------------------------------------------------------------------------------------------
Java Source Code
------------------------------------------------------------------------------------------------------
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ExecutorDemo {
 private static class MyRunnable implements Runnable {
  @Override
  public void run() {
   for (int i = 1; i <= 10; i += 2) {  //odd number generation
    try {
     Thread.sleep(500);  //executors thred sleeping
     System.out.println(i);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }

 public static void main(String[] args) {
  Runnable r = new MyRunnable();
  Executor e = Executors.newSingleThreadExecutor();  //creating a executor
  e.execute(r);  //submitting and executing the task
  for (int i = 0; i <= 10; i += 2) {  //even number generation
   try {
    Thread.sleep(500);  //main-thread sleeping
    System.out.println(i);
   } catch (InterruptedException ie) {
    ie.printStackTrace();
   }
  }
 }

}
------------------------------------------------------------------------------------------------------
Download Links
------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared

Happy coding :)

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.