Site icon

Concurrency – Producer & Consumer in java (Wait notify example)

1. DataStore class:

package org.learn.sync.wn;

import java.util.LinkedList;
import java.util.Random;

public class DataStore {
    public int storeSize;
    public LinkedList<Integer> store;

    Random random = new Random(1000);

    public DataStore(int size) {
        storeSize = size;
        store = new LinkedList<>();
    }

    public synchronized void put() {

        if(store.size() == storeSize) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        int putValue = random.nextInt(1000);
        store.offer(putValue);
        System.out.printf("Putting : Value = %d\n", putValue);
        notifyAll();
    }

    public synchronized void get() {

        if(store.size() == 0 ){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int value = store.poll();
        System.out.printf("Getting Value : %d \n",value);
        notifyAll();
    }
}

2. Producer class:

package org.learn.sync.wn;


public class Producer implements Runnable {

    private DataStore dataStore;
    private int numberOfItems;
    public Producer(DataStore dataStore, int numberOfItems) {
        this.dataStore = dataStore;
        this.numberOfItems = numberOfItems;
    }
    @Override
    public void run() {

        for(int count = 0; count < numberOfItems; count++) {
            dataStore.put();
        }
    }
}

3. Consumer class:

package org.learn.sync.wn;

public class Consumer implements Runnable {

    private DataStore dataStore;
    private int numberOfItems;

    public Consumer(DataStore dataStore, int numberOfItems) {
        this.dataStore = dataStore;
        this.numberOfItems = numberOfItems;
    }

    @Override
    public void run() {

        for(int count = 0; count < numberOfItems; count++) {
            dataStore.get();
        }
    }
}

4. WaitNotifyExample class:

package org.learn.sync.wn;

public class WaitNotifyExample {

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

        DataStore dataStore = new DataStore(10);

        Thread producer = new Thread(new Producer(dataStore, 20),"ProducerThread");
        Thread consumer = new Thread(new Consumer(dataStore, 20),"ConsumerThread");

        producer.start();
        consumer.start();

        producer.join();
        consumer.join();
    }
}

5. Output: Producer & Consumer scenario using wait & notify in java

ProducerThread, Putting : Value = 487
ProducerThread, Putting : Value = 935
ProducerThread, Putting : Value = 676
ProducerThread, Putting : Value = 124
ProducerThread, Putting : Value = 792
ProducerThread, Putting : Value = 349
ProducerThread, Putting : Value = 641
ProducerThread, Putting : Value = 845
ProducerThread, Putting : Value = 264
ProducerThread, Putting : Value = 450
ConsumerThread, Getting : Value = 487
ConsumerThread, Getting : Value = 935
ConsumerThread, Getting : Value = 676
ConsumerThread, Getting : Value = 124
ConsumerThread, Getting : Value = 792
ConsumerThread, Getting : Value = 349
ConsumerThread, Getting : Value = 641
ConsumerThread, Getting : Value = 845
ConsumerThread, Getting : Value = 264
ConsumerThread, Getting : Value = 450
ProducerThread, Putting : Value = 379
ProducerThread, Putting : Value = 159
ProducerThread, Putting : Value = 372
ProducerThread, Putting : Value = 383
ProducerThread, Putting : Value = 836
ProducerThread, Putting : Value = 475
ProducerThread, Putting : Value = 646
ProducerThread, Putting : Value = 2
ProducerThread, Putting : Value = 323
ProducerThread, Putting : Value = 241
ConsumerThread, Getting : Value = 379
ConsumerThread, Getting : Value = 159
ConsumerThread, Getting : Value = 372
ConsumerThread, Getting : Value = 383
ConsumerThread, Getting : Value = 836
ConsumerThread, Getting : Value = 475
ConsumerThread, Getting : Value = 646
ConsumerThread, Getting : Value = 2
ConsumerThread, Getting : Value = 323
ConsumerThread, Getting : Value = 241
Exit mobile version