Java Solution single buffer producer consumer problem sample _java

Source: Internet
Author: User
Tags mutex sleep

The classic producer consumer problem simulation. This program simulates the simplest case-single buffering. In order to simulate the actual situation, the consume item and the produce item were added with the delay, and the different generation consumption rate could be simulated by modifying the delay.

[Code]

[/co/**
* Single buffer consumer-producer problem.
* by Xu (xusiwei1236@163.com).
* */
public class Consumerproducer {

static Object buffer = NULL;

Static Object Mutex = new Object ();

static Object Condconsumer = new Object ();

static Object Condproducer = new Object ();

public static void Main (string[] args) {
Thread producer = new Thread () {
public void Run () {
for (int i=0; i<10; i++) {
for (int i=0; i++) {
Produce item.
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
String item = new String ("item-" + i);
System.out.println ("[producer] produced" + item);

Wait for buffer empty.
Synchronized (condproducer) {
while (buffer!= null) {
try {
Condproducer.wait ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

Put the item to buffer.
Synchronized (mutex) {
Buffer = Item;
System.out.println ("[producer] put" + Item + "to buffer.");
}

Notify consumers.
Synchronized (Condconsumer) {
Condconsumer.notify ();
}
}
}
};

Thread consumer = new Thread () {
public void Run () {
for (int i=0; i<10; i++) {
for (;;) {
Wait for item come.
Synchronized (Condconsumer) {
while (buffer = null) {
try {
Condconsumer.wait ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

Get item from buffer.
String item = NULL;
Synchronized (mutex) {
Item = (String) buffer;
buffer = NULL;
System.out.println ("[Consumer] get" + Item + "from buffer.");
}

Consume item.
try {
Thread.Sleep (500);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("[Consumer] comsumed" + item);

Notify producers.
Synchronized (condproducer) {
Condproducer.notify ();
}
}
}
};

Consumer.start ();
Producer.start ();
}
}de]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.