Collaboration between Java threads Wait () and Notifyall ()

Source: Internet
Author: User

Package Org.rui.thread.block;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import java.util.concurrent.timeunit;//Wax Wax Electrical Automatic mode public class Waxomatic {public static void main (string[] args) throw s interruptedexception {car car = new Car (); Executorservice exec = Executors.newcachedthreadpool (); Exec.execute (new Waxoff (car)); Exec.execute (new Waxon (car)); TimeUnit.SECONDS.sleep (5); Exec.shutdownnow ();//Interrupt All Tasks//Shutdownnow attempt to stop all active tasks being performed, suspend processing of the awaited task, and return to the list of tasks awaiting execution}} Class Car {//for polishing, waxing processing state private Boolean Waxon = false;//waxing public synchronized void waxed () {Waxon = true;//readiness to B Uffnotifyall ();} Polished public synchronized void buffed () {Waxon = false;//ready to another coat of Waxnotifyall (); Wait waxing public synchronized void waitforwaxing () throws Interruptedexception {while (Waxon = = false) {wait ();//suspend this task}}/ /wait polished public synchronized void waitforbuffing () throws Interruptedexception {while (Waxon = true) {wait ();//suspend this task}}}c Lass Waxon ImplemeNTS Runnable {private Car car;public Waxon (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {System.out.println ("Wax on!"); TimeUnit.MILLISECONDS.sleep (car.waxed);//waxing car.waitforbuffing ();//Polishing}} catch (Interruptedexception e) { System.out.println ("Exit by Interruption");//E.printstacktrace ();} System.out.println ("Ending Wax on Task");}} Class Waxoff implements Runnable {private Car car;public Waxoff (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {car.waitforwaxing ();//etc Wax System.out.println ("Wax off!"); TimeUnit.MILLISECONDS.sleep (+); car.buffed ();//Polishing}} catch (Interruptedexception e) {System.out.println ("Exit by Break") ;//E.printstacktrace ();} System.out.println ("Ending Wax Off task");}} /*output: (95% match) Wax On!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax Off!wax On!wax Off!wax on! via interrupt exit ending wax on task exits ending wax off task*/




Collaboration between Java threads Wait () and Notifyall ()

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.