Last active
October 9, 2018 15:23
-
-
Save isabek/3a92c85d5972d2cf45bbfd47d3208c8b to your computer and use it in GitHub Desktop.
Producer-Consumer Example in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package concurrency.pizza; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.Random; | |
class Bucket { | |
private final static Logger LOGGER = LoggerFactory.getLogger(Bucket.class); | |
private volatile String pizzaName; | |
private volatile boolean isExist = false; | |
synchronized void put(String pizzaName) { | |
while (isExist) { | |
try { | |
wait(); | |
} catch (InterruptedException e) { | |
LOGGER.error("Error occurred in put {}", e.getMessage()); | |
} | |
} | |
LOGGER.info("Producer::put({})", pizzaName); | |
this.pizzaName = pizzaName; | |
this.isExist = true; | |
notify(); | |
} | |
synchronized void get() { | |
while (!isExist) { | |
try { | |
wait(); | |
} catch (InterruptedException e) { | |
LOGGER.error("Error occurred in get {}", e.getMessage()); | |
} | |
} | |
LOGGER.info("Consumer::get({})", this.pizzaName); | |
this.pizzaName = null; | |
this.isExist = false; | |
notify(); | |
} | |
} | |
class Producer extends Thread { | |
private final static Logger LOGGER = LoggerFactory.getLogger(Producer.class); | |
private final Bucket bucket; | |
private final NameGenerator nameGenerator; | |
Producer(Bucket bucket, NameGenerator nameGenerator) { | |
this.bucket = bucket; | |
this.nameGenerator = nameGenerator; | |
} | |
@Override | |
public void run() { | |
while (!isInterrupted()) { | |
String pizza = this.nameGenerator.generate(); | |
this.bucket.put(pizza); | |
try { | |
sleep(500); | |
} catch (InterruptedException e) { | |
LOGGER.error("Error occurred {}", e.getMessage()); | |
} | |
} | |
} | |
} | |
class NameGenerator { | |
private static String names[] = {"Margherita", "Marinara", "Carbonara", "Frutti di Mare", "Quattro Formaggi", "Crudo", "Napoletana or Napoli"}; | |
private Random random = new Random(); | |
String generate() { | |
return names[random.nextInt(names.length)]; | |
} | |
} | |
class Consumer extends Thread { | |
private final static Logger LOGGER = LoggerFactory.getLogger(Consumer.class); | |
private final Bucket bucket; | |
public Consumer(Bucket bucket) { | |
this.bucket = bucket; | |
} | |
@Override | |
public void run() { | |
while (true) { | |
bucket.get(); | |
try { | |
sleep(300); | |
} catch (InterruptedException e) { | |
LOGGER.error("Error occurred {}", e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public class Pizza { | |
public static void main(String[] args) throws InterruptedException { | |
Bucket bucket = new Bucket(); | |
Thread producer = new Producer(bucket, new NameGenerator()); | |
producer.setName("Producer"); | |
Thread consumer = new Consumer(bucket); | |
consumer.setName("Consumer"); | |
producer.start(); | |
consumer.start(); | |
producer.join(); | |
consumer.join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment