Monday, December 2, 2013

Java Thread - Producer and Consumer

Making a multi-tasking application in Java has never been an easy task before. That's why they have updated the Thread framework since Java 7. Today, I will show you guys how you can achieve a multi-tasking application which is synchronized with each other called the Producer/Consumer.

Think of a Producer as a thread that supplies a stock of food to all of its clients called Consumer. It produces foods only if the Consumer needs it. Otherwise, it will not produce. The Consumer then consumes what the Producer has produce then notify it that it needs to produce.

Please examine the following codes:

package conc;

import java.util.concurrent.ArrayBlockingQueue;

public class BlockingQueue {

    public static void main(String[] args) throws Exception {

     java.util.concurrent.BlockingQueue queue = new ArrayBlockingQueue(100);

        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        new Thread(producer).start();
        new Thread(consumer).start();

        Thread.sleep(4000);
    }
}
===============================
package conc;

public class Consumer implements Runnable{

    java.util.concurrent.BlockingQueue queue = null;

    public Consumer(java.util.concurrent.BlockingQueue queue) {
        this.queue = queue;
    }

    public void run() {
        try {
         while(true){
          System.out.println("drugs taking out..");
          System.out.println("TAKE: " + queue.take());
         }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
==============================
package conc;

public class Producer implements Runnable{

 java.util.concurrent.BlockingQueue queue = null;

    public Producer(java.util.concurrent.BlockingQueue queue) {
        this.queue = queue;
    }

    public void run() {
        try {
         while(true){
          System.out.println("loading drugs..");
          queue.put("drugs loaded!"); 
          Thread.sleep(2000);
         }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
The main function makes an instance of java.util.concurrent.BlockingQueue<Stringwhich is used as a medium or buffer for the two threads. It also creates the two threads - Producer and Consumer threads and then fire it up using their respective start method. If you will try to analyze the two threads' overriden method, there is an infinite loop with two second interval, this is where the fun begins. The Producer only need to make a call queue's "put" method while the Consumer thread only needs to call the queues' "get" method and the new framework does all the heaving coding to make it synchronized for you. From the source code above, you can then make serious applications that needs a multitasking features without getting into trouble from thread coding. Have fun coding and thank you for reading my post! Till next time! :)

No comments:

Post a Comment