Java's Multi-Threading Foundation (2) consumer and producer relationships

Source: Internet
Author: User

1. Multiple threads Repeat a result as follows: It's sold out and the warehouse is full.

public class Productandconsumer {public static void main (string[] args) {Clerk Clerk = new Clerk (); Product Product = new product (clerk); Consumer Consumer = new Consumer (clerk), new thread (product), start (); new Thread (Consumer). Start ();} Class Clerk {private int goodsnumber = 0;public synchronized void get () {if (goodsnumber<=10) {System.out.println (" Item A stock quantity is: "+ ++goodsnumber);} Else{system.out.println ("The warehouse is full");}} Public synchronized void Sale () {if (goodsnumber>0) {System.out.println ("Item A inventory quantity is:" +--goodsnumber);} Else{system.out.println ("already sold Out");}}} Class Product implements Runnable{private clerk Clerk;public Product (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.get ();}}} Class Consumer implements Runnable{private clerk Clerk;public Consumer (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.sale ();}}} 

2. Multi-threaded synchronization lock caused by deadlock, as follows: Businessmen to find buyers first to money, buyers to find businessmen first to cause the deadlock, because: one does not release the lock, the other is not locked

public class Goodsandmoney {public static void main (string[] args) {Trader Trader = new Trader (Buyer.money); Buyer buyer = New Buyer (trader.goods); new Thread (Trader). Start (); new Thread (buyer). Start ();} Class Trader implements Runnable {public static string goods = "goods";p ublic static string Money;public Trader (String mon EY) {Trader.money = money;} @Overridepublic void Run () {while (true) {synchronized (goods) {synchronized (money) {}}system.out.println ("pay first ~ after delivery ~");} }class Buyer implements Runnable {public static string money = ' money ';p ublic static string Goods;public buyer (String good s) {buyer.goods = goods;} @Overridepublic void Run () {while (true) {synchronized (money) {try {Thread.Sleep ($)} catch (Interruptedexception e) {E . Printstacktrace ();} Synchronized (goods) {}}system.out.println ("first delivery ~ after paying ~");}}}

3. False wakeup, as follows: When the last occurrence is sold out, the selling thread is in the wait state, so the thread does not end

public class Productandconsumer {public static void main (string[] args) {Clerk clerk = new Clerk (); Product Product = new product (clerk); Consumer Consumer = new Consumer (clerk), new thread (product), start (); new Thread (Consumer). Start ();} Class Clerk {private int goodsnumber = 0;public synchronized void get () {if (goodsnumber<1) {System.out.println (" Commodity a inventory quantity is: "+ ++goodsnumber); This.notifyall ();} Else{system.out.println ("Warehouse is full"), try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Public synchronized void Sale () {if (goodsnumber>0) {System.out.println ("Item A inventory quantity is:" +--goodsnumber); This.notifyall ();} Else{system.out.println ("Sold Out"), try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}}}} Class Product implements Runnable{private clerk Clerk;public Product (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.get ();}}} Class Consumer implements Runnable{private clerk Clerk;public Consumer (clerk clerk) {THIS.CLerk = Clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.sale ();}}}

4. False wakeup, when a thread enters wait, it releases the thread's lock, the subsequent thread is likely to wait, and after it wakes up, two threads run out of range

Package Volatile1;public class Productandconsumer {public static void main (string[] args) {Clerk clerk = new Clerk (); Product Product = new product (clerk); Consumer Consumer = new Consumer (clerk); new Thread (product). Start (); new Thread (Consumer). Start (); new Thread (product). Start (); New Thread (consumer). Start ();}} Class Clerk {private int goodsnumber = 0;public synchronized void get () {if (Goodsnumber >= 5) {System.out.println ("Warehouse has "), try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} SYSTEM.OUT.PRINTLN ("Commodity A Inventory Quantity:" + ++goodsnumber); This.notifyall ();} Public synchronized void Sale () {if (goodsnumber <= 0) {System.out.println ("sold Out"), try {this.wait ();} catch (Interrup Tedexception e) {e.printstacktrace ();} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Commodity a stock Quantity:" +--goodsnumber); This.notifyall ();}} Class Product implements Runnable {private Clerk clerk;public Product (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.get ();}}} Class Consumer implements Runnable {private Clerk clerk;public Consumer (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.sale ();}}}

5. Adding a loop to the 4 code will resolve the spurious wake-up, as the wake-up will pass while the condition needs to be met to jump out, and the first time to the while value will change, so it will not appear full

Package Volatile1;public class Productandconsumer {public static void main (string[] args) {Clerk clerk = new Clerk (); Product Product = new product (clerk); Consumer Consumer = new Consumer (clerk); new Thread (product). Start (); new Thread (Consumer). Start (); new Thread (product). Start (); New Thread (consumer). Start ();}} Class Clerk {private int goodsnumber = 0;public synchronized void get () {while (Goodsnumber >= 5) {System.out.println (" The warehouse is full "); try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Commodity A Inventory Quantity:" + ++goodsnumber); This.notifyall ();} Public synchronized void Sale () {goodsnumber <= 0) {System.out.println ("sold Out"), try {this.wait ();} catch (Inter Ruptedexception e) {e.printstacktrace ();} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Commodity a stock Quantity:" +--goodsnumber); This.notifyall ();}} Class Product implements Runnable {private Clerk clerk;public Product (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.get ();}}} Class Consumer implements Runnable {private Clerk clerk;public Consumer (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.sale ();}}}

5.jdk1.5 later lock method to resolve the above problems

Package Volatile1;public class ProductAndConsumer2 {public static void main (string[] args) {Clerk clerk = new Clerk (); Product Product = new product (clerk); Consumer Consumer = new Consumer (clerk); new Thread (product). Start (); new Thread (Consumer). Start (); new Thread (product). Start (); New Thread (consumer). Start ();}} Class Clerk {private int goodsnumber = 0;public synchronized void get () {while (Goodsnumber >= 5) {System.out.println (" The warehouse is full "); try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Commodity A Inventory Quantity:" + ++goodsnumber); This.notifyall ();} Public synchronized void Sale () {goodsnumber <= 0) {System.out.println ("sold Out"), try {this.wait ();} catch (Inter Ruptedexception e) {e.printstacktrace ();} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} SYSTEM.OUT.PRINTLN ("Commodity a stock Quantity:" +--goodsnumber); This.notifyall ();}} Class Product implements Runnable {private Clerk clerk;public Product (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.get ();}}} Class Consumer implements Runnable {private Clerk clerk;public Consumer (clerk clerk) {This.clerk = clerk;} @Overridepublic void Run () {for (int i = 0; i <; i++) {Clerk.sale ();}}}

  

Java's Multi-Threading Foundation (2) consumer and producer relationships

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.