Sunday, April 24, 2011

Practical demonstration of parallel processing Using Multiple threads in java

I  read most of the published java literature on java concurrency,but still felt "I could sell it but not build it" .
So I chose to close the books and write something that made sense of all the big promises .

Simple Idea: Say if my process needs to add a billion numbers ,can i make it faster ,by dividing its workload into "N" Threads.
Sample code and o/p below to demonstrate the benefits of multi threading .

import java.util.concurrent.CountDownLatch;
public class AddBillionNumbers
{

static long count =0;
public static long addSequentially(){
for(long i=1;i<=1000000000;i++){
count += i;
}
return count;
}
public static void main( String args[]){

//Sequential Single Threaded Approach

long start = System.currentTimeMillis();

System.out.println("Sum of a billion numbers is : "+addSequentially());

long end = System.currentTimeMillis();

System.out.println("Single Threaded Computation performed in : "+ (end - start)+ " Ms\n");



//Multithreaded Approach

long start2 = System.currentTimeMillis();
CountDownLatch doneSignal = new CountDownLatch(6);

/*

* I am trying to break up the computation process into 6 threads .

* Thread1 will add Numbers 1,7,13,19...

* Thread2 will add Numbers 2,8,14,20....

* ...

* Thread6 will add Numbers 6,12,18,24.....

* Note the diffrence between the start and jump indexes are chosen to

* make sure no 2 threads end up adding the same number.

* I am using a CountDownLatch to know immediately when all my threads have finished as

* opposed to checking isAlive individually on each thread.

*/



//Executor e = Executors.newFixedThreadPool(6);

for (int i = 1; i <= 6; ++i) // create and start threads

{

new Thread((new HelperThread(doneSignal,i,6))).start();

}

try {

doneSignal.await();

} catch (InterruptedException ie) {

ie.printStackTrace();

}

System.out.println("Sum of a billion numbers is : "+ HelperThread.computationCount);

long end2 = System.currentTimeMillis();

System.out.println("MultiThreaded Computation performed in : "+ (end2 - start2)+ " Ms");


}

}

class HelperThread extends Thread{



int startIndex,jumpIndex;

static long computationCount = 0;

String threadName;

private final CountDownLatch doneSignal;


public HelperThread(CountDownLatch doneSignal,int startIndex, int jumpIndex)

{

this.doneSignal = doneSignal;
this.startIndex = startIndex;
this.jumpIndex = jumpIndex;
}

public void run() {

count(startIndex, jumpIndex);

doneSignal.countDown();

}

public void count(int startIndex, int jumpIndex){

long count = 0;

for(int i=startIndex;i<=1000000000;i+=jumpIndex){

count += i;

}

synchronized(HelperThread.class){

computationCount += count;

}

}

}


O/P below on 32 bit 4Processor 3GB RAM Windows
Sum of a billion numbers is : 500000000500000000
Single Threaded Computation performed in : 2901 Ms


Sum of a billion numbers is : 500000005000000000
MultiThreaded Computation performed in : 485 Ms