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