Java threading – The Executor framework

The main goal of this article is to present the better mechanism of executing threads in Java and to show that working directly with threads is no longer necessary. Thanks to the Executor framework, working with threads in Java is easier. And there are people who claim that working with threads in Java is easier than in most other languages.

Defining threads in Java

Multithreading is increasingly becoming the  part of every modern application these days. The program is multithreaded if it is able to run more than one thread at the same time.

In Java, defining threads is straightforward and it is done either by extending the java.lang.Thread class and overriding its run() method or, more common, by implementing the java.lang.Runnable interface.

Let’s look at the example of defining a thread by implementing the Runnable interface:

class SomeRunnable implements Runnable {
    public void run() {
	for (int i = 0; i < 3; i++) {
	    System.out.println(Thread.currentThread().getName() + " counter="
		    + i);

To have the above code run by a separate thread the new java.lang.Thread object must be instantiated and started as it is shown below:

public class Runner {
    public static void main(String[] args) {
	// instantiate a runnable object
	Runnable runnable = new SomeRunnable();
	// instantiate 2 threads and pass the runnable object
	Thread thread_0 = new Thread(runnable);
	Thread thread_1 = new Thread(runnable);
	// start the threads

Using the Runnable interface is better than extending the Thread class. One of the main advantages of doing that is the ability to use an Executor framework instead of directly using the Thread class to create and execute new threads: constructing new threads is relatively expensive because it involves interaction with the operating system.

The Executor framework

The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies.

The Executor Framework was introduced in Java 1.5 and it is a part of the java.util.concurrent package. The main interface for the framework is the Executor interface and its subinterfaces: ExecutorService and ScheduledExecutorService. Please refer to Executor API [ExecutorAPI] to see the detailed description of the interfaces.

Let’s take a look at the simple example of using the Executor framework to run a previously created task using a fixed thread pool:

public class Runner {
    public static void main(String[] args) {
	Runnable runnable = new SomeRunnable();
	// create a fixed thread pool
	ExecutorService pool = Executors.newFixedThreadPool(3);
	// run the task 5 times using the pool
	for (int i = 0; i < 5; i++) {

In the above example, I used the ExecutorService interface which is an Executor that provides a number of useful methods to control the task execution. For the creation of the concrete ExecutorService, I used the Executors class and one of its static factory methods: newFixedThreadPool(). Please refer to Executor API [ExecutorAPI].

Task scheduling

The ScheduledExecutorService is the Executor that supports scheduling of tasks based on the relative time or to run task periodically. Example of scheduling a task to run after a 1000 ms:

public class Runner {
    public static void main(String[] args) {
	Runnable runnable = new SomeRunnable();

	ScheduledExecutorService thread = Executors

	thread.schedule(runnable, 1000, TimeUnit.MILLISECONDS);


The article barely scratched the surface of the Executor framework. Thus, I encourage the reader to study the framework’s documentation to see its power and simplicity.

References and useful links

  1. [ExecutorAPI] – The API documentation of the framework is comprehensive and should be a good starting point to get along with the framework. Examples included –
  2. “Concurrency: Executors” article at
  3. “The Executor Framework” from “Java Concurrency in Practice” at


Aspire Blog Team

Aspire Systems is a global technology services firm serving as a trusted technology partner for our customers. We work with some of the world's most innovative enterprises and independent software vendors, helping them leverage technology and outsourcing in our specific areas of expertise. Our services include Product Engineering, Enterprise Solutions, Independent Testing Services and IT Infrastructure Support services. Our core philosophy of "Attention. Always." communicates our belief in lavishing care and attention on our customers and employees.


  1. Rafal,

    nice useful article – congratulations.

    I am curious how efficient Executor framework will be working with Swing and AWT GUIs which are using own threads ?


  2. Rafal,

    nice useful article – congratulations.

    I am curious how efficient Executor framework will be working with Swing and AWT GUIs which are using own threads ?



    Fast delivery, No hidden fees, Moneyback guarantee. Order in bulk and get the lowest price.

    + Get FREE pills with your order
    + 24/7/365 Customer Support
    + Top quality medications
    + All most payment methods
    + Fast Worldwide shipping

    Order Generic Lopid Here

    About Generic Lopid
    Gemfibrozil helps reduce cholesterol and triglycerides (fatty acids) in the blood. High levels of these types of fat in the blood are associated with an increased risk of atherosclerosis (clogged arteries). Gemfibrozil is used to treat very high cholesterol and triglyceride levels in people with…

    Related search:
    side effects lopid
    is lopid a statin
    rash lopid
    stop taking lopid
    lopid liver bamage
    reactions to lopid
    lopid 900
    lopid and ceftin drug interactions
    lopid cholesterol drug
    get lopid
    lopid drug test
    lopid drug reactions
    zocor lopid
    lopid uses
    reactions to lopid
    lopid dosage
    drug interactions norvasc zetia lopid
    lopid vc
    buy generic lopid
    diflucan and lopid inteaction
    lopid liver law
    celebrex and lopid
    lopid 900
    side effects lopid
    rash from lopid
    lopid manufacturer
    side effects of lopid
    can’t tolerate statins or lopid
    lopid price
    lipitor and lopid
    lopid dangers
    lopid foods to avoid
    lopid manufactuer
    medication lopid 25 mg
    lopid and pregnancy
    celebrex and lopid
    spanish word for lopid
    lopid dosage
    lopid 600mg tab
    triglyceride drug lopid
    lopid and lactation
    lopid gemfibrozil

  4. [url=]] [b]Buy Actonel lowest price! [/b][/url]


    [b]* Customer support available 24/7![/b]
    [b]* No prescription required![/b]
    [b]* Payment options: Visa, Amex, MasterCard, eCheck, Wire transfe etc[/b].

    [url=]] [b]Click Here [/b][/url]

    Weighty tidings here Actonel
    Do not take a Actonel tablet if you cannot seating for stand-up or rise after at least 30 minutes. Actonel can give rise to life-threatening problems in the abdomen or esophagus (the tube that connects your way out and suffer). You will lack to stay upright as a replacement for at least 30 minutes after winsome this medication.
    Take the Actonel capsule before all emotional attachment in the morning, at least 30 minutes prior to you sup or drink anything or take any other medicine.

    womens discussion group on actonel
    actonel fosamax vs
    [url=] availability of risedronate actonel in us [/url]
    [url=] actonel jaw problems [/url]
    [url=] mdl fosamax actonel 1789 [/url]
    [url=] actonel information sheet [/url]
    [url=] i can’t afford actonel [/url]
    [url=] food and drug administration actonel [/url]
    [url=] alendronate fosamax and risedronate actonel revisited [/url]
    [url=] actonel litigation [/url]
    [url=] actonel homeopathy [/url]
    [url=] generic actonel [/url]
    [url=] actonel vertebral fracture 31 [/url]
    [url=] actonel and jaw problems [/url]
    [url=] actonel with calcium [/url]
    [url=] actonel eyes [/url]
    [url=] define actonel [/url]
    [url=] side effects to the drug actonel [/url]
    [url=] actonel fatigue [/url]
    [url=] taking actonel and prevacid [/url]
    [url=] actonel and heartburn [/url]

    [url=] actonel drug [/url]
    [url=] actonel 2 tablets monthly [/url]
    [url=] missing a dose of actonel [/url]
    [url=] fosamax or actonel [/url]
    [url=] actonel with calcium [/url]
    [url=] actonel vs alendronate [/url]
    [url=] actonel and pain discussion [/url]
    [url=] actonel product [/url]
    [url=] aventis poland actonel [/url]
    [url=] choosing between fosamax actonel or boniva [/url]
    [url=] can valerian root interfere with actonel [/url]
    [url=] suma flax seed actonel taken together [/url]
    [url=] ocular side effects actonel [/url]

    [url=] cognitive impairment from actonel [/url]
    [url=] order actonel online [/url]
    [url=] actonel vs boniva [/url]
    [url=] actonel alternative [/url]
    [url=] actonel et creatinine [/url]
    [url=] long term effects of taking actonel [/url]
    [url=] actonel 150mg [/url]
    [url=] actonel vs evista [/url]
    [url=] evista or actonel [/url]
    [url=] * Actonel no prescription discount * Actonel no prescription online * Actonel online * cheap Actonel online * buy Actonel pill * Actonel medicine * Actonel pill * order Actonel medication * buy Actonel online * Actonel discount * buy Actonel pill * Actonel pharmacy online * Actonel no prescription discount * Actonel pills online * order Actonel no prescription * cheap Actonel no prescription * purchase Actonel * purchase Actonel no prescription * purchase Actonel [/url]
    [url=] actonel vs evista [/url]
    [url=] actonel joint pain [/url]
    [url=] actonel pros and cons [/url]

    [url=] who owns actonel [/url]
    [url=] find actonel serious side effects [/url]
    [url=] actonel side affects [/url]
    [url=] actonel news alerts [/url]
    [url=] vaginal bleeding with actonel [/url]
    [url=] side effects of the drug actonel [/url]
    [url=] actonel cost [/url]
    [url=] actonel mdl [/url]
    [url=] alternative drug to actonel [/url]
    [url=] actonel boniva fosamax side effects [/url]
    [url=;action=post;title=StartNewTopic] buy actonel sales online specials [/url]
    [url=,989.0.html] actonel and dental extraction osteomyelitis rcdso [/url]
    [url=] actonel womens forum [/url]

    [url=] actonel jaw [/url]
    [url=] actonel one weekly side effects [/url]
    [url=] order actonel online [/url]
    [url=] order actonel [/url]
    [url=] discontinuing therapy with actonel [/url]
    [url=] actonel jaw problems [/url]
    [url=] buy overnight cheap actonel [/url]
    [url=] actonel generic [/url]
    [url=] buy actonel online cheap [/url]
    [url=] marketing actonel [/url]
    [url=] actonel tb [/url]
    [url=] who owns actonel [/url]
    [url=] actonel commercial [/url]

    [url=] can i take mobic and actonel [/url]
    [url=] medicarion actonel [/url]
    [url=] actonel medication [/url]
    [url=] actonel mayo clinic [/url]
    [url=] actonel not working [/url]
    [url=] missing a dose of actonel [/url]
    [url=] jaw problems actonel [/url]
    [url=] side-effects of actonel [/url]
    [url=] buy cheap buy actonel online [/url]
    [url=] actonel side effects after two years [/url]
    [url=] actonel risedronate [/url]
    [url=,] actonel vs fosamax which is better [/url]
    [url=] actonel speakers [/url]

    [url=] actonel and jaw problems [/url]
    [url=] actonel vs evista [/url]
    [url=] i can’t afford actonel [/url]
    [url=] actonel otosclerosis [/url]
    [url=] choosing between fosamax actonel or boniva [/url]
    [url=] actonel jaw problems [/url]
    [url=] actonel and side affects [/url]
    [url=] actonel warnings [/url]
    [url=] forum actonel [/url]
    [url=] actonel and managed care [/url]
    [url=] buy actonel online rezeptservice [/url]
    [url=,3446.0.html] actonel and vitamin d deficiency [/url]
    [url=] what is actonel [/url]

  5. Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum!

Comments are closed.