Concurrency is the ability to run several parts of a program or several programs in parallel. If time consuming tasks can be performed asynchronously or in parallel, this improve the throughput and the interactivity of your program.
Executors framework (java.util.concurrent.Executor), released with the JDK 5 in package java.util.concurrent is used to run the Runnable objects without creating new threads every time and mostly re-using the already created threads.
Thread pools manage a pool of worker threads. The thread pools contains a work queue which holds tasks waiting to get executed.
A thread pool can be described as a collection of
Runnable objects (work queue) and a connections of running threads. These threads are constantly running and are checking the work query for new work. If there is new work to be done they execute this Runnable. The Thread class itself provides a method, e.g. execute(Runnable r) to add a new
Runnable object to the work queue.
The Executor framework provides example implementation of the java.util.concurrent.Executor interface, e.g. Executors.newFixedThreadPool(int n) which will create n worker threads. The ExecutorService adds lifecycle methods to the Executor, which allows to shutdown the Executor and to wait for termination.
Callable interface : (click here to see comparision of callable vs runnable)There are two disadvantages with using the Runnable interface in JAVA:
- Cannot return value from method run
- Cannot throw Checked Exception
Step 1:
import java.util.concurrent.Callable;
@SuppressWarnings("rawtypes")
public class CallingThread implements Callable{
int x;
public CallingThread(int x) {
this.x=x;
}
@Override
public Object call() throws Exception {
//Do some Business Processing here
//created thread calls the Add funcion of Businessprogram class. (5 times it called, because in the MainThread class we have created 5 threads
System.out.println("Thread No "+x); //
return x; //return statement is mandatory for the call method.
}
}
Step 2: Main program implimenting executor framework
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainThread{
private static ExecutorService service = Executors.newFixedThreadPool(10); //connection pool
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InterruptedException {
List<CallingThread> threads = new ArrayList<CallingThread>(); //ArrayList to store the threads with CallingThread Type
CallingThread ct = null;
for(int i=1;i<=5;i++){ // 5 threads are created
ct = new CallingThread(i); //calling CallingThread constructor of the CallingThread class.
threads.add(ct); // Storing the threads with theirs tasks
}
service.invokeAll((Collection<? extends Callable<CallingThread>>) threads); //invoking all the threads using connection pool
}
}
Ouput:Total =30 (you will get five times, since we have created 5 threads here).
Thanks for Reading. Your comments are welcome.