- Creating Threads by implementing the Callable Interface
- Using the Executor Framework in Java
Implementing the Callable Interface
import java.util.concurrent.Callable;
class CallableTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
return sum;
}
}
Creating the Threads and running them
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableInterfaceDemo {
public static void main(String[] args) {
FutureTask<Integer>[] futureList = new FutureTask[5];
for (int i = 0; i <= 4; i++) {
Callable<Integer> callable = new CallableTask();
futureList[i] = new FutureTask<Integer>(callable);
Thread t = new Thread(futureList[i]);
t.start();
}
for (int i = 0; i <= 4; i++) {
FutureTask<Integer> result = futureList[i];
try {
System.out.println("Future Task" + i + ":" + result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
Callable<Integer> callable = new CallableTask();
futureList[i] = new FutureTask<Integer>(callable);
Thread t = new Thread(futureList[i]);
Getting the result from the Thread
FutureTask<Integer> result = futureList[i];
try {
System.out.println("Future Task" + i + ":" + result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Complete Code
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class CallableTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
return sum;
}
}
public class CallableInterfaceDemo {
public static void main(String[] args) {
FutureTask<Integer>[] futureList = new FutureTask[5];
for (int i = 0; i <= 4; i++) {
Callable<Integer> callable = new CallableTask();
futureList[i] = new FutureTask<Integer>(callable);
Thread t = new Thread(futureList[i]);
t.start();
}
for (int i = 0; i <= 4; i++) {
FutureTask<Integer> result = futureList[i];
try {
System.out.println("Future Task" + i + ":" + result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
The Executor Framework
How to use the Executor Framework
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class Worker implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
return sum;
}
}
public class ExecutorDemo {
public static void main(String[] args) {
ExecutorService executors = Executors.newFixedThreadPool(4);
Future<Integer>[] futures = new Future[5];
Callable<Integer> w = new Worker();
try {
for (int i = 0; i < 5; i++) {
Future<Integer> future = executors.submit(w);
futures[i] = future;
}
for (int i = 0; i < futures.length; i++) {
try {
System.out.println("Result from Future " + i + ":" + futures[i].get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} finally {
executors.shutdown();
}
}
}
ExecutorService executors = Executors.newFixedThreadPool(4);
Future<Integer> future = executors.submit(w);
Getting the result of the Thread
try {
System.out.println("Result from Future " + i + ":" + futures[i].get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Some possible Scenarios of the Fixed Thread Pool
- In this example we created a fixed Thread pool of size 4
- If we submit a total of 3 tasks to this ExecutorService, then all 3 tasks will be assigned to the thread pool and they will start executing.
- If we submit 4 tasks to this ExecutorService, then again all 4 tasks will be assigned to the thread pool and they will start executing.
- Now if we submit 5 tasks to this thread pool. Only 4 of the tasks will be assigned to the thread pool. This is because the size of the Thread pool is 4. The 5th Task will be assigned only when one of the threads in the pool gets freed
Shutting down the ExecutorService
executors.shutdown();
Code
Congrats😃
Feel free to connect with me in LinkedIn or follow me in Twitter