Java programming-synchronized (2) and synchronized

Source: Internet
Author: User

Java programming-synchronized (2) and synchronized

One room is shared by one and two, which share a bathroom object. This requires the synchronized keyword. When one and two use the bathroom at the same time, one requires wait () waiting to be awakened, the other bathroom object is released after use. At this time, the used object needs to enter the wait () state. Otherwise, the deadlock may occur. After the toilet resource is released, you also need to wake up another thread that is listening to this object.

First, construct a Room class Room, and internally construct objects 1 and 2. An Object is a bathroom class, which is used as the class to be monitored by the thread. Use the synchronized keyword:

synchronized (toliet) {}

In this case, the toliet object is shared in {} and can be used for objects 1 and 2. At the same time, the statement is in the run () method so that no deadlock occurs during object 1 and 2 calls.

  

/** Rent a room in the same room as No. 1 and No. 2, use a toilet together, brush your teeth, and urinate, end */class Room extends Thread {static Room p1 = new Room ("1"); static Room p2 = new Room ("2"); private String name; // code: static Object toliet = new Object (); Room (String name) {this. name = name ;}@ Override public void run () {// TODO Auto-generated method stub synchronized (toliet) {// only obtain the bathroom lock, in order to obtain the use of resources in the bathroom if (this. name. equals ("1") {this. brush (); try {toliet. wait (); // wait for waking up at the door after brushing your teeth. this. relase (); // urinate after being awakened, and wake up toliet 2 after urinating. Y ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} else {this. brush (); toliet. Y (); // Let No. 1 in and wait until it is awakened. try {toliet. wait (); this. relase ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}}// method of brushing teeth public void brush () {System. out. println (this. name + "brushing your teeth"); try {Thread. sleep (2000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (this. name + "brush your teeth");} // public void relase () {System. out. println (this. name + "urinating"); try {Thread. sleep (2000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (this. name + "completed urination") ;}} public class start {public static void main (String [] args) {// TODO Auto-generated method stub Room. p1.start (); Room. p2.start ();}}

The simple synchronized keyword is used. The two threads Listen to an object together. Do not confuse the logical expression. One thread is awakened, and the other enters the wait () State immediately. The complete code is shown above, at the same time, be aware that the thread is awakened or enters the wait () state. It is the thread that is being monitored and the thread that is using this object. For example:

Toliet. wait (); // wait for waking up at the door after brushing your teeth. this. relase (); // urinate after being awakened, and wake up toliet 2 after urinating. notify ();

Yes <Object> toliet. wait () and toliet. Allow y (). If this is used here, or another thread Object is used, a deadlock will occur.

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.