Java Multithreading: Producer-Consumer (Video Tutorial Part 7)

A tutorial on how to implement the producer-consumer pattern in Java using the ArrayBlockingQueue Java class. Producer-Consumer is the situation where one or more threads are producing data items and adding them to a shared data store of some kind while one or more other threads process those items, removing them from the data store.

After starting the video, click the maximise button to make it fullscreen so you can see the code!



Code For This Tutorial

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


public class App {
    
    private static BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);
    
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    producer();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    consumer();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        
        t1.start();
        t2.start();
        
        t1.join();
        t2.join();
    }
    
    private static void producer() throws InterruptedException {
        Random random = new Random();
        
        while(true) {
            queue.put(random.nextInt(100));
        }
    }
    
    private static void consumer() throws InterruptedException {
        Random random = new Random();
        
        while(true) {
            Thread.sleep(100);
            
            if(random.nextInt(10) == 0) {
                Integer value = queue.take();
                
                System.out.println("Taken value: " + value + "; Queue size is: " + queue.size());
            }
        }
    }
}
 
Taken value: 59; Queue size is: 10
Taken value: 40; Queue size is: 10
Taken value: 74; Queue size is: 9
Taken value: 40; Queue size is: 9
Taken value: 88; Queue size is: 9
Taken value: 74; Queue size is: 9
Taken value: 96; Queue size is: 9
Taken value: 88; Queue size is: 10
Taken value: 34; Queue size is: 10
Taken value: 81; Queue size is: 10
Taken value: 44; Queue size is: 10
Taken value: 84; Queue size is: 10
Taken value: 23; Queue size is: 10
Taken value: 77; Queue size is: 9
Taken value: 33; Queue size is: 9
Taken value: 45; Queue size is: 10
Taken value: 61; Queue size is: 9
Taken value: 24; Queue size is: 9
Taken value: 9; Queue size is: 9
Taken value: 70; Queue size is: 9
Taken value: 52; Queue size is: 9
Taken value: 70; Queue size is: 9
Taken value: 69; Queue size is: 9