Self explanatory Producer and Consumer problem
Producer.java
package com.producer; import java.util.ArrayList; import java.util.Random; public class Producer implements Runnable{ //Engine Batch ID reference ArrayList engineList; //Engine No is stored here String str; //Assigning the BATCH ID reference public Producer(ArrayList engineList) { this.engineList = engineList; } /* * Produce Engines */ public void produce(){ //Random number for engine number str = ""+Math.abs(new Random(System.currentTimeMillis()).nextLong()); //Only one thread can enter the BATCH for any operation synchronized (engineList) { //Add the Engine to the batch engineList.add(str); //Notify all other threads that the new engine is added to the BATCH engineList.notifyAll(); } //Print Engine Number System.out.println("Produced :: ENGINE CHASIS NO ["+ str + "]"); } @Override public void run() { System.out.println("Production started"); //10 Engine is to be produced for(int i=0;i<10;i++){ try { //Produce the engine this.produce(); //Sleep for 2 seconds for the next operation Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Consumer.java
package com.producer; import java.util.ArrayList; public class Consumer extends Thread{ //Engine Batch ID reference ArrayList<String> engineList; //Engine No is stored here String str; //Assigning the BATCH ID reference public Consumer(ArrayList<String> engineList) { this.engineList = engineList; } /* * Consume Engines from the BATCH */ public void consume(int index) throws InterruptedException{ //Only one thread can enter the BATCH for any operation synchronized (engineList) { //if the Batch has no engines while(engineList.size() == 0){ //then wait for some time until you get a new engine to consume engineList.wait(); } //If the engine is deployed to the BATCH if(engineList.size() >= 0){ //The consume the engine from the respective position str = engineList.get(index); } } //Print Engine Number that it has been consumed System.out.println("Consumed :: -------> ENGINE CHASIS NO ["+ str + "]" ); } @Override public void run() { //10 Engine has to be consumed for(int i=0;i<10;i++){ try { //Produce the engine this.consume(i); //Sleep for 3 seconds for the next operation Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Main.java
package com.producer; import java.util.ArrayList; public class Main { public static void main(String[] args) throws InterruptedException { //ArrayList to store the produced engines - BATCH ArrayList<String> engine = new ArrayList<String>(); //Producer taking the engine BATCH ID Reference Producer producer = new Producer(engine); //Consumer taking the reference of the engine BATCH ID Reference Consumer consumer = new Consumer(engine); //Threads Thread prodThread = new Thread(producer); Thread conThread = new Thread(consumer); //Starting the production prodThread.start(); //Start Consumption conThread.start(); } }