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.
------------------------------------------------------------------------------------------------------
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 :)
Labels:Threads
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment