The synchronized of the method of the Java common face test __java the difference between the lock pool and the waiting pool

Source: Internet
Author: User


When a thread enters the synchronized method A of an object, whether other threads can enter synchronized method B of this object.


No. Other threads can access only the asynchronous method of the object, and the synchronization method cannot enter. Because the synchronized modifier on a non-static method requires that the object's lock be obtained when the method is executed, the thread attempting to enter the B method can only wait for the object's lock in the lock pool (note that it is not the waiting pool) if the object lock has been taken away.


In Java , each object has two pools, a lock (monitor) pool, and a wait pool

Wait (), Notifyall (), notify () Three methods are all methods in the object class.

Lock pool: Suppose thread A already has a lock on an object (note: Not a Class), and other threads want to invoke some synchronized method (or synchronized block) of the object. Because these threads must acquire the lock ownership of the object before it enters the object's synchronized method, the lock of the object is currently owned by thread A, so the threads enter the lock pool of the object.

Wait pool: Assuming that a thread a invokes the waiting () method of an object, thread a releases the lock on the object (since the wait () method must appear in synchronized so that thread a already has a lock on the object before executing the Wait () method). At the same time, thread A enters the waiting pool of the object. If another thread calls the Notifyall () method of the same object, the thread in the waiting pool of the object will all go into the lock pool of the object, ready to scramble for the lock's ownership. If another thread calls the Notify () method of the same object, then only a thread (random) in the waiting pool of the object enters the lock pool of the object.

Here is an example to illustrate:

Requires two threads to be written, one thread adds the value of one member variable of an object to 1, and the other thread cuts the value of the member variable by 1. Makes the value of the variable always in [0,2]. The initial value is 0.

[Java] view plain copy package com.tju;   class target   {        private int count;               public synchronized void increase ()        {            if (count == 2)             {                try               {                   wait ();                }                 catch  (interruptedexception e)       &Nbsp;         {                    e.printstacktrace ();                }            }           count++;            system.out.println (Thread.CurrentThread (). GetName ()  +  ":"  +  Count);           notify ();        }              public synchronized  Void decrease ()        {            if (count == 0)            {               try                {                    //wait, as decrease thread calls this method,                    //so the decrease thread enters the wait pool of object T (instantiated in the main function) and frees the lock of the object T                     Wait (); method of//object class                }                catch  (interruptedexception  e)                {                    E.printstacktrace ();               }            }           count--;           system.out.println (Thread.CurrentThread (). GetName ()  +   ":"  + count);                       //Wake thread increase,increase thread from wait pool to lock pool             notify ();       }  }   class  increase extends thread   {       private Target t;          

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: 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.