Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Executor Framework in Java: Take Control Over Your Tasks

DZone 's Guide to

Executor Framework in Java: Take Control Over Your Tasks

Want to learn more about using the executor framework in Java? Check out this post to learn more about this framework through a series of examples.

· Java Zone ·
Free Resource

What You Will Learn

  • What are Executors in Java?
  • What are the advantages of using Executors in Java?
  • How Executors address certain weaknesses of the Thread class API
  • How we can use Executors to fine-tune the execution of thread pools
  • How Executors allow threads to synchronize with each other

Prerequisites

  • The meaning terms such as thread, synchronization, scheduling, and time-slice

Tools You Will Need

  • JDK Environment installed
  • A code editor or an IDE, such as Eclipse

Threads and Concurrency

Any program that runs on a single-processor machine, goes through with its code in a single sequence. Concurrency changes the single sequence part, by providing the concept of a thread. A thread consists of a single sequence of instructions, so it's similar in meaning to a program. What stands out, however, is that independent threads can be executed concurrently. In programming terms, this means that even though our main program runs on a single processor, individual threads within it could achieve progress.

The Java Run-time has a thread scheduler running to help with this. Whatever time-slice the OS scheduler gives to a program, is split among its threads by the thread scheduler. What follows is processor time-sharing, and these threads overlap with each other as they run. If certain parts of a program need to wait for an external event, other parts that do not, could be allowed to proceed.

Introducing the Executors Framework

In order to address some serious limitations of the Thread API, a new Executor Service was added in Java SE 5. The ExecutorService is a framework you can use to better manage threads in your code. It contains built-in methods that:

  • Create and launch threads more intuitively
  • Manage thread state and its life-cycle more easily
  • Synchronize between threads with more control
  • Handle groups of threads neatly

The ExecutorService provides utilities to achieve each one of these. Let's start with thread creation, which is demonstrated in the following example.

Example 1: Creating a Thread With Executor Service

//**_ExecutorServiceRunner.java_**
import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

class Task1 extends Thread {

public void run() {

System.out.println("Task1 Started ");

for (int i = 101; i <= 199; i++) {

System.out.print(i + " ");

}

System.out.println("\nTask1 Done");

}

}

class Task2 implements Runnable {

@Override

public void run() {

System.out.println("Task2 Started ");

for (int i = 201; i <= 299; i++) {

System.out.print(i + " ");

}

System.out.println("\nTask2 Done");

}

}

public class ExecutorServiceRunner {

public static void main(String[] args) {

ExecutorService executorService = Executors.getNewSingleThreadExecutor();

executorService.execute(new Task1());

executorService.execute(new Thread(new Task2()));

}

}


Console Output

Task1 Started

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

Task1 Done

Task2 Started

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

Task2 Done

Explained:

  • An ExecutorService instance is returned by the the Executors framework class. On this reference, invoking getNewSingleThreadExecutor() returns a reference to a single, manageable thread. Sure, this code will create a single sub-task, so it is equivalent to a single-threaded program. But, doesn't the code look neater and more compact, already?

More on Executors

Now, let's have a look at the real thing, creating more than one thread using theExecutorService!

Example 2: Running Threads Concurrently Using Executor Service

public class ExecutorServiceRunner {

public static void main(String[] args) {

ExecutorService executorService = Executors.getNewSingleThreadExecutor();

executorService.execute(new Task1());

executorService.execute(new Thread(new Task2()));

System.out.print("\nTask3 Kicked Off\n");

for (int i = 301; i <= 399; i++) {

System.out.print(i + " ");

}

System.out.println("\nTask3 Done");

System.out.println("\nMain Done");

executorService.shutdown();

}

}


Explained:

  • The only order in the resulting chaos is: Task2 starts its execution only after Task1 is complete. This tells us that a SingleThreadExecutor service can be used to control thread execution but only for threads availing its services! The thread running main() are out of bounds for the ExecutorService.

Customizing Number Of Threads

With the ExecutorService, it is possible to create a pool of threads. Such a group of threads shares similar properties regarding the ways in which you can control and manage them. Normally, you can pass in a parameter to a factory method that creates this thread pool, which indicates its size.

The following examples will show you how you can create thread pools of varying kinds and, of course, different sizes.

Example 3: Executors for Concurrent Threads

//**_ExecutorServiceRunner.java_**
public class ExecutorServiceRunner {

public static void main(String[] args) {

ExecutorService executorService = Executors.getNewFixedThreadPool(2);

executorService.execute(new Task1());

executorService.execute(new Thread(new Task2()));

System.out.print("\nTask3 Kicked Off\n");

for (int i = 301; i <= 399; i++) {

System.out.print(i + " ");

}

System.out.println("\nTask3 Done");

System.out.println("\nMain Done");

executorService.shutdown();

}

}


Explained:

By using an ExecutorService of type FixedThreadPool:

  • Task1 and Task2 execute concurrently as part of the ExecutorService.
  • The thread running main() executes concurrently with this thread pool, created by the ExecutorService.

More on Thread Pools

Let's explore one more scenario involving thread pool creation.

Example 4: All-Executor Task Execution

//**_ExecutorServiceRunner.java_**
import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

class Task extends Thread {

private int number;

public Task(int number) {

this.number = number;

}

public void run() {

System.out.println("Task " + number + " Started");

for (int i = number * 100; i <= number * 100 + 99; i++) {

System.out.print(i + " ");

}

System.out.println("\nTask " + number + " Done");

}

}

public class ExecutorServiceRunner {

public static void main(String[] args) {

ExecutorService executorService = Executors.getNewFixedThreadPool(2);

executorService.execute(new Task(1));

executorService.execute(new Task(2));

executorService.execute(new Task(3));

executorService.shutdown();

}
}


Explained:

  • Note that we have now created all sub-tasks as threads, which belong to the same thread pool.
  • The thread new Task(3) is executed only after any one of new Task(1) and new Task(2) have completed their execution.

Example 5: Larger Thread Pool Size

public class ExecutorServiceRunner {

public static void main(String[] args) {

ExecutorService executorService = Executors.getNewFixedThreadPool(3);

executorService.execute(new Task(1));

executorService.execute(new Task(2));

executorService.execute(new Task(3));

executorService.execute(new Task(4));

executorService.execute(new Task(5));

executorService.execute(new Task(6));

executorService.execute(new Task(7));

executorService.shutdown();

}
}


Explained:

When a larger pool size is selected, say 3, an interesting scenario plays out:

  • Initially, Tasks 1, 2, 3 are added to the ExecutorService and are started.
  • As soon as any one of them is terminated, another task from the thread pool is marked for execution, and so on. A classic case of musical chairs, with regard to the slots available in the ExecutorService thread pool, is played out here!

Summary

In conclusion, we have learned about the following concepts when using the Executors framework:

  • You can create pools of threads of the same kind, using the ExecutorService.
  • One could specify the size of a thread pool as different from the executor threads.
Topics:
executorservice ,java ,threads ,thread pool ,executor ,executor framework

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}