Java Producer Consumer Mode code example

Source: Internet
Author: User
Package test;

Import java.util.LinkedList;
		public class Test {public static void main (string[] args) {r r = new R ();
		P p1 = new P (r,10);
		P p2 = new P (r,20);
		P p3 = new P (r,30);
		P P4 = new P (r,40);
		
		P P5 = new P (r,50);
		c C1 = new C (r,10);
		c C2 = new C (r,20);
		c C3 = new C (r,30);
		c C4 = new C (r,40);
		
		c C5 = new C (r,50);
		New Thread (p1). Start ();
		

		New Thread (C1). Start ();
		New Thread (P2). Start ();
		

		New Thread (C2). Start ();
		New Thread (P3). Start ();
		

		New Thread (C3). Start ();
		New Thread (P4). Start ();
		

		New Thread (C4). Start ();
		New Thread (P5). Start ();
	New Thread (C5). Start ();
	} class P implements runnable{private R R;
	private int n;
		Public P (R _r,int _n) {THIS.R = _r;
	THIS.N = _n;
	@Override public void Run () {R.push (n);
	Class C implements runnable{private R R;
	private int n;
		Public C (R _r,int _n) {THIS.R = _r;
	THIS.N = _n;
	@Override public void Run () {R.pop (n); } class R {private Linkedlist<object> C = new linkedlist<object> ();
	private static final int max_size = 100; public void push (int n) {synchronized (c) {if (C.size () +n <= max_size) {for (int i=0;i<n;i++) {C.add (NE
				W Object ()); System.out.println ("Producer: Production of" +n+ "products, there are now a total of" +c.size () + "products, and wake up the consumer line program control can be consumed ....
				");
			C.notifyall (); }else{System.out.println ("Producer: number of products to be produced:" +n+ ", greater than the remaining space in the warehouse" + (Max_size-c.size ()) + "Current producer threads enter the waiting state ....
				");
				try {c.wait ();
				catch (Interruptedexception e) {e.printstacktrace (); }}} public void pop (int n) {synchronized (c) {if (n <= c.size ()) {for (int i=0;i<n;i++) {C
				. remove (); System.out.println ("Consumer: Consumption of" +n+ "products, there are still" +c.size () + "products, and wake-up producer line program control can be produced ....
				");
			C.notifyall (); }else{System.out.println ("Consumers: the number of products to be consumed:" +n+ ", greater than the number of warehouse surplus" +c.size () + "current consumer thread into the waiting state ....
				");
				try {c.wait ();
				catch (Interruptedexception e) {e.printstacktrace ();
}
			}		}
	}
} 

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.