Producer and Consumer Problem


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();

}

}


Advertisements

18 thoughts on “Producer and Consumer Problem

  1. Shelby Tekulve

    I just want to tell you that I am just all new to blogs and really liked you’re blog. Very likely I’m likely to bookmark your blog post . You absolutely come with amazing stories. With thanks for sharing with us your webpage.

    Reply
  2. Mauricio Pletz

    I simply want to mention I’m newbie to blogs and seriously loved you’re web site. Most likely I’m likely to bookmark your blog . You amazingly come with impressive articles and reviews. Bless you for sharing your webpage.

    Reply
  3. Pingback: Nuclear Power Stations | Electrical Project Superintendent

  4. Yajaira Yacavone

    Hi! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any methods to protect against hackers?

    Reply
  5. Infomedia Uptodate

    Wow! This can be one of the most helpful blogs we have ever come across on thesubject. Actually magnificent info! I’m also an expert in this topic so I can understand your hard work.

    Reply
  6. Mccubbin

    Hi there webmaster – That is absolutely the best searching site I’ve found. It had been entirely simple to understand and it was easy to look with the info which i required. Fantastic web design and really good content material!

    Reply
  7. how to get a free evo 4g

    naturally like your web-site but you have to test the spelling on several of your posts. A number of them are rife with spelling problems and I to find it very bothersome to inform the reality nevertheless I will definitely come again again.

    Reply
  8. tarot gitano

    Everything is very open with a precise explanation of the issues. It was definitely informative. Your website is extremely helpful. Thank you for sharing!

    Reply
  9. no credit check cash loans

    Howdy! This post could not be written any better! Going through this post reminds me of my previous roommate! He continually kept preaching about this. I will send this article to him. Fairly certain he will have a very good read. I appreciate you for sharing!

    Reply
  10. 3g mobile games

    Aw, this was an exceptionally good post.
    Finding the time and actual effort to generate a very good article… but what can I say… I hesitate a lot and never
    seem to get anything done.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s