Hanumant’s Java Workshop

Turbo Charged Java Development!

Parallel Computing in Java

I have been reading a bit about how to make our Java applications scalable. Besides standard performance techniques that one can apply to fine tune one’s application, I was also trying to find out what I can do if my Java application is performing at its best but it is not enough. What if one server is just not enough to perform a task in the time required by an SLA ? How to employ multiple machines to perform such a task? This is different from Clustering which has to be done at the application server level and seems more suitable for “load balancing” kind of requirement. It can be used to do multiple tasks at multiple places but cannot be used to do one task at multiple places.

Enter, Terracotta and GridGain.
While Terracotta allows you to make your objects shared accross JVMs, GridGain seems to be a more pure parallel computing type of environment. Apparently, both of these tools can be used to make your application take advantages of multiple machines.

The Approach
To understand how they work, I am going to implement at simple producer-consumer scenario where there are producers of Jobs and consumers that take up those Jobs. It is the standard multi-threaded producer-consumer scenario except that the we are going to have the consumers (and the producers as well, if required) running on multiple machines instead of multiple threads on one machine. The idea is to employ multiple machines to do the jobs instead of multiple threads working on the same machine.

Let’s see some code now …

The basic producer consumer scenario –

First let’s define what our producers and consumers will work on —

A Job :

//imports 
public class Job { 
    int jobduration = (int) (Math.random()*5000); 
    public void run(){ 
        try { 
            Thread.sleep(jobduration); 
            System.out.println("Job finished in " + jobduration + " millis."); 
        } catch (InterruptedException ex) { 
            ex.printStackTrace(); 
        } 
    } 
}

Jobs – a container to hold all the jobs that need to be done :

//imports 
public class Jobs { 
    //we can also use BlockingQueue and avoid writing our own synchronization logic 
    //Come to think of it, if we use BlockingQueue, we won't need Jobs class at all. 
    //But this is good for learning the Terracotta stuff. 
    private Queue list = new LinkedList();   

    public Job getJob() { 
        while(true) 
        { 
            synchronized(this) 
            { 
                try{ 
                    if(!list.isEmpty()) return list.remove(); 
                    else this.wait(); 
                }catch(Exception e){ 
                    e.printStackTrace(); 
                } 
            } 
        } 
    }   

    public void addJob(Job job) { 
        synchronized(this){ 
            list.offer(job); 
            this.notifyAll(); 
        } 
    } 
}

Let’s now look at the producer and the consumer code.

The producer :

//imports... 
public class Producer extends Thread { 
    Jobs jobs; 
    public Producer(Jobs j){ 
        jobs = j; 
    }   

    public void run(){ 
        while(true){ 
            try { 
                //sleep randomly for up to 5 seconds. 
                Thread.sleep((int) (Math.random()*5000)); 
                jobs.addJob(new Job()); 
            } catch (InterruptedException ex) { 
                ex.printStackTrace(); 
            } 
        } 
    } 
}


The consumer :

//imports... 
public class Consumer extends Thread{ 
    Jobs jobs; 
    public Consumer(Jobs j){ 
        jobs = j; 
    }   

    public void run(){ 
        while(true){ 
            Job job = jobs.getJob(); 
            if(job!=null) job.run(); 
        } 
    } 
}

In a regular, single JVM application, we would create a shared Jobs instance and create as many number of Producer and Consumer threads as we want passing them the same Jobs instance.

For example:

public class OldMain { 
    Jobs jobs = new Jobs(); 
    public OldMain(){ 
        new Producer(jobs).start(); 
        new Consumer(jobs).start(); 
        new Consumer(jobs).start(); 
    }   

    public static void main(String[] args){ 
        new OldMain(); 
    } 
}

Here, we are running two Consumers in the same JVM, which doesn’t really add much value unless it is running on multiple CPUs. So, what we want to do is to run Consumers on multiple machines, while all picking up jobs from the same Jobs instances.

September 26, 2007 - Posted by | Java

18 Comments »

  1. “Parallel Computing in Java Hanumants Java Workshop” was indeed a remarkable article, can not help but wait to read through far more of ur articles.
    Time to squander several time online haha. Thanks ,
    Sol

    Comment by toyark.mobi | December 31, 2012 | Reply

  2. I desire to book mark this specific post, “Parallel Computing in Java Hanumants Java
    Workshop” on my personal blog. Would you care in case I personallydo it?
    Thanks ,Daisy

    Comment by http://tinyurl.com/haliprice24687 | January 23, 2013 | Reply

  3. It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I want to suggest you few interesting things or tips. Maybe you could write next articles referring to this article. I want to read even more things about it!

    Comment by seo company | March 21, 2013 | Reply

  4. I love reading a post that can make people
    think. Also, thank you for allowing for me to comment!

    Comment by boiler installation | June 1, 2013 | Reply

  5. I am actually thankful to the owner of this website who has shared this great
    paragraph at at this time.

    Comment by fat burning foods | August 19, 2013 | Reply

  6. Oh my goodness! Impressive article dude! Thanks, However I am going through
    issues with your RSS. I don’t know why I can’t join it. Is there anyone else
    getting identical RSS problems? Anyone who knows the solution can you
    kindly respond? Thanx!!

    Comment by veterinary assistant schools | April 18, 2014 | Reply

  7. I am regular visitor, how are you everybody? This article posted at this web site is in fact nice.

    Comment by Minecraft free download deutsch | July 24, 2014 | Reply

  8. Excellent article. Keep posting such kind of
    info on your page. Im really impressed by it.

    Hi there, You have done an incredible job. I will definitely digg it and in my opinion recommend to my friends.
    I’m sure they’ll be benefited from this site.

    Comment by gåNedivekt.Com | July 27, 2014 | Reply

  9. That is a really good tip particularly to those new to the blogosphere.
    Brief but very precise info… Appreciate your sharing this one.
    A must read post!

    Comment by http://GåNedivekt.com/slankekur/nyttige-triks-slanke-program-2/ | July 27, 2014 | Reply

  10. Fastidious answer back in return of this query with
    firm arguments and explaining all concerning that.

    Comment by Http://GåNedivekt.Com/Slanketips/Slanketips-Som-Funker-De-Beste-Tipsene/ | July 30, 2014 | Reply

  11. You ought to take part in a contest for one of the highest quality blogs on the web.
    I will highly recommend this site!

    Comment by http://gånedivekt.com/slankekur/tips-triks-mest-effektive-slankekur/ | August 1, 2014 | Reply

  12. Having read this I believed it was rather enlightening.
    I appreciate you spending some time and effort to put this informative article
    together. I once again find myself personally spending way too
    much time both reading and posting comments. But
    so what, it was still worthwhile!

    Comment by teespring marketing | August 23, 2014 | Reply

  13. Since the admin of this site is working, no question very rapidly it will be
    renowned, due to its feature contents.

    Comment by Emmett | September 1, 2014 | Reply

  14. Hi there, I do believe your web site could be having browser compatibility
    issues. Whenever I take a look at your blog in Safari, it
    looks fine however when opening in IE, it’s got some overlapping issues.
    I merely wanted to give you a quick heads up! Besides that, great
    site!

    Comment by videomakerfx review | October 1, 2014 | Reply

  15. That is a good tip especially to those new to the blogosphere.
    Short but very precise info? Appreciate your sharing this one.
    A must read post!

    Comment by diastolic blood pressure | June 26, 2016 | Reply

  16. Its not my first time to pay a quick visit this web site, i am browsing this website dailly and take pleasant facts from here all the time.

    Comment by shopping blog | February 13, 2017 | Reply

  17. I read this post completely about the resemblance of hottest and earlier technologies,
    it’s awesome article.

    Comment by blogs | April 20, 2017 | Reply

  18. I visit day-to-day some blogs and blogs to read
    posts, but this blog presents quality based posts.

    Comment by amra-amrai.com | September 24, 2017 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: