Ask:
When a thread enters the synchronized method A of an object, whether other threads can enter synchronized method B of this object.
For:
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.
Expand:
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;