I have written the below sample program using normal way and the multithreading way using callable interface to check the execution speed. But I am able to see the shocking result, which is nothing but single thread is executing more more faster than multithreading. But I understood that multithreading is good technique can be used for complex business process and batch jobs.
Multithreading:
[java]
package runnableandcallable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class MainThread {
private static ExecutorService service = Executors.newFixedThreadPool(10); // connection
// pool
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InterruptedException {
System.out.println("Multithreading");
long startTime = System.nanoTime();
long lStartTime = new Date().getTime();
MainThread mt = new MainThread();
mt.testThread(1000, 200);
long lEndTime = new Date().getTime();
long difference = lEndTime – lStartTime;
System.out.println("Elapsed milliseconds: " + difference);
long EndTime = System.nanoTime();
System.out.println("Elapsed Time :::"
+ ((EndTime – startTime) / 1000000));
}
public void testThread(final int a, final int b) {
// create a callable for each method
Callable<Void> callableAdd = new Callable<Void>() {
@Override
public Void call() throws Exception {
Add(a, b);
return null;
}
};
Callable<Void> callableSub = new Callable<Void>() {
@Override
public Void call() throws Exception {
Sub(a, b);
return null;
}
};
Callable<Void> callableMul = new Callable<Void>() {
@Override
public Void call() throws Exception {
Mul(a, b);
return null;
}
};
Callable<Void> callableDiv = new Callable<Void>() {
@Override
public Void call() throws Exception {
Div(a, b);
return null;
}
};
// add to a list
List<Callable<Void>> taskList = new ArrayList<Callable<Void>>();
taskList.add(callableAdd);
taskList.add(callableSub);
taskList.add(callableMul);
taskList.add(callableDiv);
// create a pool executor with 3 threads
ExecutorService executor = Executors.newFixedThreadPool(3);
try {
// start the threads
List<Future<Void>> futureList = executor.invokeAll(taskList);
for (Future<Void> voidFuture : futureList) {
try {
// check the status of each future. get will block until the
// task
// completes or the time expires
voidFuture.get(100, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
System.err
.println("Error executing task " + e.getMessage());
} catch (TimeoutException e) {
System.err.println("Timed out executing task"
+ e.getMessage());
}
}
} catch (InterruptedException ie) {
// do something if you care about interruption;
}
}
private void Add(int a, int b) {
System.out.println("Add :::" + (a + b));
}
private void Sub(int a, int b) {
System.out.println("Sub :::" + (a – b));
}
private void Mul(int a, int b) {
System.out.println("Multiply :::" + (a * b));
}
private void Div(int a, int b) {
System.out.println("Division :::" + (a / b));
}
}
[/java]
Output:
[plain]
Multithreading
Sub :::800
Division :::5
Add :::1200
Multiply :::200000
Elapsed milliseconds: 6
Elapsed Time :::6
[/plain]
Single Thread Program:
[java]
import java.util.Date;
public class NormalJava {
public static void main(String[] args) {
System.out.println("Single Thread");
long startTime = System.nanoTime();
long lStartTime = new Date().getTime();
int a = 1000;
int b = 200;
NormalJava nj = new NormalJava();
nj.Add(a, b);
nj.Sub(a, b);
nj.Mul(a, b);
nj.Div(a, b);
long lEndTime = new Date().getTime();
long difference = lEndTime – lStartTime;
System.out.println("Elapsed milliseconds: " + difference);
long EndTime = System.nanoTime();
System.out.println("Elapsed Time :::"
+ ((EndTime – startTime) / 1000000));
}
private void Add(int a, int b) {
System.out.println("Add :::" + (a + b));
}
private void Sub(int a, int b) {
System.out.println("Sub :::" + (a – b));
}
private void Mul(int a, int b) {
System.out.println("Mul :::" + (a * b));
}
private void Div(int a, int b) {
System.out.println("Mul :::" + (a / b));
}
}
[/java]
Output:
[plain]
Single Thread
Add :::1200
Sub :::800
Mul :::200000
Mul :::5
Elapsed milliseconds: 0
Elapsed Time :::0
[/plain]
Use multithreading always for complex business logics and business processes to see the real purpose of multithreading.
Recommended Books:
Buy Java Thread Programming: The Authoritative Solution from Flipkart.com
Buy Java Concurrency in Practice 1st Edition from Flipkart.com