Wednesday, 27 April 2016

Simple producer and consumer problem in java (1.8)


Simple producer and consumer problem solved in java (1.8) with help of BlockingQueue 


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

/**
 * Created by vijaysy on 26/04/16.
 */
public class ProducerConsumerApp {

    public static void main(String[] args) {
        //new Thread(() -> {System.out.printf("Hi");}).start();
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(i);
                    blockingQueue.put(String.format("%s",i));
                    System.out.println("Produced:" + i);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                blockingQueue.put("exit");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {
            String string;
            try {
                while ((string = blockingQueue.take()) != "exit") {
                    Thread.sleep(20);
                    System.out.println("Consumed: " + string);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }).start();
        
    }

}