threads

Thread communication using Queue example

This is an example of how to achieve a queue communication between two Threads. The example is described in short:

  • We have created a class, PrepProduct that implements the Runnable and has a BlockingQueue of Strings. It overrides the run() API method of the Runnable where
    it puts two elements to the BlockingQueue, waiting if necessary for space to become available, with put(String e) API method of BlockingQueue.
  • We have also created a class Production that implements the Runnable, and also has a BlockingQueue of Strings. It overrides the run() API method of the Runnable where it retrieves and removes the head of this queue, waiting if necessary until an element becomes available, using take() API method of BlockingQueue.
  • We create a new BlockingQueue of Strings and two Threads initialized with the two Runnable objects created above. We call their start() API methods to begin their execution and then their join() API method that waits for each thread to die. The first thread puts two strings in the BlockingQueue and then the other thread retrieves them from the queue. This is how the communication is accomplished through the threads.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;



class PrepProduct implements Runnable {

    BlockingQueue<String> blQueue;

    PrepProduct(BlockingQueue<String> bqueu) {

  blQueue = bqueu;
    }

    @Override
    public void run() {

  

  try {


blQueue.put("1");


blQueue.put("end");

  } catch (Exception e) {


e.printStackTrace();

  }
    }
}


class Production implements Runnable {

    private final BlockingQueue<String> queue;

    Production(BlockingQueue<String> bqueue) {

  queue = bqueue;
    }

    @Override
    public void run() {

  try {


String value = queue.take();


while (!value.equals("end")) {


    value = queue.take();


    System.out.println(value);


}

  } catch (Exception e) {


System.out.println(Thread.currentThread().getName() + " "



  + e.getMessage());

  }
    }
}

public class QueueCommunication {

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

  

  BlockingQueue<String> q = new LinkedBlockingQueue<String>();

  Thread prep = new Thread(new PrepProduct(q));

  Thread prod = new Thread(new Production(q));

  

  prep.start();

  prod.start();

  prep.join();

  prod.join();

  
    }
}

Output:

end

  
This was an example of how to achieve a queue communication between two threads in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button