Learn about the wait () and notify () Usage of Java through multi-threaded programs for consumers and producers

Source: Internet
Author: User

Warehouse Class

 Public classStore {Private intsize = 0;//Current Capacity    Private Final intMAX = 10;//Maximum Capacity//adding goods to the warehouse     Public synchronized voidAdd () { while(Size >= MAX)//each execution is checked for full        {            Try{wait ();//if full, wait for the waiting pool to enter}Catch(interruptedexception e) {e.printstacktrace (); }} size++;//put the goods//Output Related informationSystem.out.println ("*************add***************");        System.out.println (Thread.CurrentThread (). toString ()); System.out.println ("Size:" +integer.tostring (size)); //Wake up the consumer thread in the waiting poolnotify (); }        //the method of taking the goods from the warehouse     Public synchronized voidRemove () { while(size = = 0)//Check the warehouse for any goods before each execution        {            Try{wait ();//If the warehouse is empty, enter the waiting pool}Catch(interruptedexception e) {e.printstacktrace (); }} size--;//take the goods.//Output Related informationSystem.out.println ("*************remove***************");        System.out.println (Thread.CurrentThread (). toString ()); System.out.println ("Size:" +integer.tostring (size)); //Wake up the producer process in the wait poolnotify (); }         Public intGetSize () {return  This. Size; }}

Producer Class

 Public classProducerextendsthread{PrivateStore S;  PublicProducer (Store s) { This. S =s; } @Override Public voidrun () { while(true) {s.add ();//Increase Cargo            Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}

Consumer class

 Public classCustomerextendsthread{Store S;  PublicCustomer (Store s) { This. S =s; } @Override Public voidrun () { while(true) {s.remove ();//take the goods.            Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}

Main

 Public classMain { Public Static voidMain (string[] args) {Store s=NewStore (); Producer P1=NewProducer (s); Producer P2=NewProducer (s); Producer P3=NewProducer (s); Customer C1=NewCustomer (s); Customer C2=NewCustomer (s); P1.setname ("Producer1"); P2.setname ("Producer2"); P3.setname ("Producer3"); C1.setname ("Customer1"); C2.setname ("Customer2");        P1.start ();        P2.start ();        P3.start ();        C1.start ();    C2.start (); }}

Output (partial)

add***************Thread[producer1,5, main] Size:1*************remove***************Thread[customer2,5, main] Size:0*************add***************Thread[producer3,5, main] Size:1*************add***************Thread[producer2,5, main] Size:2*************remove***************Thread[customer1,5, main] Size:1*************add***************Thread[producer2,5, main] Size:2*************remove***************Thread[customer1,5, main] Size:1*************remove***************Thread[customer2,5, main] Size:0*************add***************Thread[producer1,5, main] Size:1*************add***************Thread[producer3,5, main] Size:2*************add***************Thread[producer2,5, main] Size:3*************remove***************Thread[customer2,5, main] Size:2*************add***************Thread[producer3,5, main] Size:3*************add***************Thread[producer1,5, main] Size:4*************remove***************Thread[customer1,5, main] Size:3*************add***************Thread[producer2,5, main] Size:4*************add***************Thread[producer1,5, main] Size:5*************remove***************Thread[customer1,5, main] Size:4*************add***************Thread[producer3,5, main] Size:5*************remove***************Thread[customer2,5, main] Size:4*************add***************Thread[producer2,5, main] Size:5*************remove***************Thread[customer1,5, main] Size:4*************add***************Thread[producer3,5, main] Size:5*************remove***************Thread[customer2,5, main] Size:4*************add***************Thread[producer1,5, main] Size:5

Wait () lets you hold the current object into a wait state, waiting for notify () to wake up.

Learn about the wait () and notify () Usage of Java through multi-threaded programs for consumers and producers

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.