Java program design of the housing synchronized (ii)

Source: Internet
Author: User

One and second share a room, which shared a bathroom object, which is to use the Synchronized keyword, a and second when using the toilet, a wait () waiting to be awakened, the other after the use of the toilet object is released, this time just used to enter wait () State, otherwise it will cause a deadlock, and after the toilet resource is released, another thread that is listening on this object needs to be awakened.

First constructs a room class rooms, the interior constructs one number and the second number object, an object a bathroom class, treats as the thread listens the class, uses the Synchronized keyword:

synchronized (Toliet) {}

At this point, the Toliet object is shared {} and can be used for number one and number second, while the statement is inside the run () method, so that no deadlock occurs when the number one and number second objects are called.

  

 /** One and number second with one room, common use of a toilet, first brushing, in the urine, the end*/classTheextendsthread{StaticP1 =NewRoom ("one")); StaticP2 =NewRoom ("Second"); PrivateString name;//Code        StaticObject Toliet =NewObject (); The (String name) { This. Name =name; } @Override Public voidrun () {//TODO auto-generated Method Stub        synchronized(Toliet) {//only get the bathroom lock, you can get the use of resources in the bathroom            if( This. Name.equals ("Number One"))){                 This. Brush (); Try{toliet.wait (); //waiting to be awakened at the door after brushing                     This. relase ();//Wake up and start urinating, wake up number second when you're done urinatingtoliet.notify (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }Else{                 This. Brush (); Toliet.notify ();//let a number come in, and wait until you wake up.                Try{toliet.wait ();  This. relase (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }        //How to brush your teeth     Public voidBrush () {System.out.println ( This. name+ "Brushing Your Teeth"); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ( This. name+ "Finish Your Teeth"); }        //Peeing     Public voidrelase () {System.out.println ( This. name+ "is urinating"); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ( This. name+ "Pee Complete"); }} Public classStart { Public Static voidMain (string[] args) {//TODO auto-generated Method StubRoom.p1.start ();    Room.p2.start (); }}

Very simple synchronized keyword use, two threads to listen to an object together, note that the logical expression does not appear chaotic, one thread is awakened, the other immediately into the wait () state, which is the complete code, at the same time to note that the thread is awakened, or into the wait () state, Is the shared object that wakes the thread that is listening and the thread that is using the object, such as:

                    Toliet.wait ();  // waiting to be awakened at the door after brushing                    this. relase (); // Wake up and start urinating, wake up number second                    when you're done urinating Toliet.notify ();

is <object>toliet.wait () and toliet.notify (), if this is used here, or another thread object is replaced, a deadlock occurs.

Java program design of the housing synchronized (ii)

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.