Java-wait ()/notify ()

Source: Internet
Author: User

1. Wait () Usage: Wait () is wrapped in a while statement because some other task may suddenly get in and take the order when Waitperson is awakened;

2. Wait (), notify () and Notifyall () can only be called in the synchronous control method or in the synchronization control block;

Importjava.util.concurrent.*; Public classMeal {Private Final intOrdernum;  PublicMeal (intordernum) {             This. ordernum=Ordernum; }         PublicString toString () {return"Meal" +Ordernum; }} Public classRestaurant {Meal Meal; Executorservice exec=Executors.newcachedthreadpool (); Waitperson Waitperson=NewWaitperson ( This); Chef Chef=NewChef ( This);  PublicRestaurant () {exec.execute (Waitperson);    Exec.execute (chef); }} Public classWaitpersonImplementsrunnable{PrivateRestaurant Restaurant;  PublicWaitperson (Restaurant r) {Restaurant=R; } @Override Public voidrun () {Try{             while(!thread.interrupted ()) {                synchronized( This) {                     while(restaurant.meal==NULL) {wait ();//For the chef to produce a meal}} System.out.println ("Waitperson got" +restaurant.meal); synchronized(restaurant.chef) {restaurant.meal=NULL;                 Restaurant.chef.notifyAll (); }            }        }Catch(interruptedexception e) {System.out.println ("Waitperson interrupted"); }        }} Public classChefImplementsRunnable {PrivateRestaurant Restaurant; Private intCount=0;  PublicChef (Restaurant r) {Restaurant=R; } @Override Public voidrun () {Try{             while(!thread.interrupted ()) {                synchronized( This){                     while(restaurant.meal!=NULL) {wait (); }                }                                if(++count==10) {System.out.println ("Out of Food.closing");                Restaurant.exec.shutdownNow (); } System.out.print ("Order up!"); synchronized(Restaurant.waitperson) {restaurant.meal=NewMeal (count);                Restaurant.waitPerson.notifyAll (); }                                //TimeUnit.MILLISECONDS.sleep (+);            }        }Catch(interruptedexception e) {System.out.println ("Chef interrupted"); }    }}

Java-wait ()/notify ()

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.