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();
}
}
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.
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.
Pingback: Nuclear Power Stations | Electrical Project Superintendent
This is one of the classic interview question related to multithreading. while sharing critical resource between multiple threads we also need to understand how to avoid deadlock in java.
Javin
Thats an interesting idea. I would have to agree it. Thanks for sharing.
Thats an amazing idea. I would have to agree it. Thanks for sharing.
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?
as long as they get him back im happy with the move
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.
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!
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.
An cool post right there mate ! Thanks for the post .
Everything is very open with a precise explanation of the issues. It was definitely informative. Your website is extremely helpful. Thank you for sharing!
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!
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.
Superb, what a blog it is! This webpage provides valuable information to us,
keep it up.
Hi there, always i used to check web site posts here in the early
hours in the break of day, since i love to gain knowledge of more and more.
The Timberwolves used the picks to select Rubio Rubio in Washington, and today home of splendor, scored 13 points and 14 assists and 6 rebounds, and led to a 17-2 score climax.
nike free run shoes http://www.iskaashi.net/us44/