Java Multithreading implements---classic issues for producers and consumers

Source: Internet
Author: User

A few days ago the teacher led the study of a single thread and multi-threaded topics.

Here is the operating system of very classic topics, producers and consumers of the problem, here is the warehouse,

Only one person (producer or consumer) enters, and the other one waits.

The focus here is on the issue of the value of the pass. Be sure to keep the same, otherwise, there may be multiple occurrences of the objects that are stored and taken.


//========================================================================//

Warehouse Class

//========================================================================//


Package house;


Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;


public class warehouse{


Arraylist<integer> products = NULL;

Private final int maxn = 20;
Public WareHouse () {
Products = new arraylist<> ();
}

public void Add (int number) {
try {
Synchronized (Products) {
if (Products.size () < MAXN) {
System.out.printf ("The producer has deposited a number%d item \ n" in the warehouse);
Products.add (number);
System.out.printf ("The Warehouse has the CCP:" + products.size () + "a \ n");
Products.notify ();
}else if (products.size () = = MAXN) {
System.out.println ("The warehouse is full, please wait for the consumer to take away the merchandise!!!" ");
Products.wait ();
}
}
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

public void get (int number) {
try {
Synchronized (Products) {
if (products.size () = = 0) {
System.out.println ("The warehouse is empty, please be patient and wait for the product to be added!! ");
Products.wait ();

}else if (products.size () > 0) {
System.out.printf ("The consumer has removed a product numbered%d", Products.get (0));
Products.remove (0);
System.out.println ("The Warehouse has the CCP:" + products.size () + "a \ n");
Products.notify ();
}
}
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}


//===============================================================//


Consumer Category:

//==========================================================================//




Package house;


Import Java.util.Random;


public class Customer implements Runnable {
Random ran = new random ();
Public WareHouse WA;
public int name;
Public Customer (WareHouse wa, int name) {
TODO auto-generated Constructor stub
This.wa = WA;
THIS.name = name;
}
@Override
public void Run () {
TODO auto-generated Method Stub
for (int i = 0; i <; i++) {
int getnumber = Ran.nextint (600);
Wa.get (GetNumber);
}

}


}


//=====================================================================================//

Producer Class

//=====================================================================================//


Package house;


Import Java.util.Random;


public class Producer implements Runnable {
Random ran = new random ();
Public WareHouse WA;
public int name;

Public Producer (WareHouse wa, int name) {
TODO auto-generated Constructor stub
This.wa = WA;
THIS.name = name;
}
@Override
public void Run () {
TODO auto-generated Method Stub
for (int i = 0; i <; i++) {
int addnumber = Ran.nextint (500);
Wa.add (Addnumber);
}

}


}



//======================================================================================//

Test class

//========================================================================================//

Package house;


public class Test {


public static void Main (string[] args) {
TODO auto-generated Method Stub

SYSTEM.OUT.PRINTLN ("Consumer and producer wars begin----->>>>>>");
WareHouse wa = new WareHouse ();
Producer p = new Producer (WA, 1);
Customer C = new Customer (WA, 1);
Thread P1 = new Thread (p);
P1.start ();
Thread C1 = new Thread (C);
C1.start ();
SYSTEM.OUT.PRINTLN ("Consumer and producer's resource preemption end----->>>>>>");
}


}


Java Multithreading implements---classic issues for producers and consumers

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.