using BlockingQueue in threads - Java Thread

Java examples for Thread:BlockingQueue

Description

using BlockingQueue in threads

Demo Code



import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Main {
    int nextInitial = 1;
    private int initialCapacity = 10;
    private BlockingQueue<Integer> list = new ArrayBlockingQueue<Integer>(
            initialCapacity);/*from   w w w .  ja  v  a 2s.  c o m*/

    public static void main(String[] args) {
    Main threadSignaling = new Main();
    Thread publisher1 = new Thread(() -> threadSignaling.publisher());
    publisher1.start();
    Thread publisher2 = new Thread(() -> threadSignaling.publisher());
    publisher2.start();
    Thread publisher3 = new Thread(() -> threadSignaling.publisher());
    publisher3.start();

    Thread subscriber1 = new Thread(() -> threadSignaling.subscriber());
    subscriber1.start();
    Thread subscriber2 = new Thread(() -> threadSignaling.subscriber());
    subscriber2.start();
    Thread subscriber3 = new Thread(() -> threadSignaling.subscriber());
    subscriber3.start();

}

    public void subscriber() {
        int message = 0;
        try {
            message = list.take();
        } catch (InterruptedException e) {
        }
        System.out.println(Thread.currentThread().getName() + " "
                + nextInitial + ". Message subscribed..." + message);
    }

    public void publisher() {

        try {
            list.put(nextInitial);
        } catch (InterruptedException e) {
        }
        System.out.println(Thread.currentThread().getName() + " "
                + nextInitial + ". Message published..." + nextInitial);
        nextInitial++;
    }
}

Related Tutorials