简体   繁体   中英

Which Thread Hold Lock in wait() and notify() multithreading

I have a confusion in multithreading in below code ...

public class ThreadTest implements Runnable {
        private int num;
        private static SecondThread obj = new SecondThread();


        public void run() {
            synchronized (obj) { //which thread holding lock here 
                try {

                    obj.wait();   

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }   
            }   
        }   
    }
  1. which thread is owner while synchronize method is called.
  2. which thread holding lock ThreadTest or SecondThread
  3. what is mean by releasing lock here obj.wait(); means ? Once lock is released can same object come inside synchronized block and access code until previous thread get notified ?

From the Javadocs (bolding mine):

 public final void wait(long timeout) throws InterruptedException 

...

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object ; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

So, in your example:

  1. A thread enters your run() method, then waits to get a lock on obj .
  2. Once it gets that lock, it enters obj.wait() and releases its lock on obj() .
  3. When it wakes up (for one of the four reasons above), it waits to get a lock on obj before returning from obj.wait() .

The answer to "Which thread holds the lock?" depends entirely on when you examine the object. If all your threads are blocking in obj.wait() waiting to wake up, then no thread holds the lock.

  1. Which thread is owner while synchronize method is called.

The thread that entered the synchronized block.

  1. Which thread holding lock ThreadTest or SecondThread

The thread that entered the synchronized block.

  1. What is mean by releasing lock here obj.wait(); means?

It means that the lock is released. It seems perfectly clear. Unclear what you're asking.

Once lock is released can same object come inside synchronized block and access code

Yes.

until previous thread get notified?

Until the thread that is in wait() exits wait() .

1.which thread is owner while synchronize method is called. 2.which thread holding lock ThreadTest or SecondThread

Ans. The thread which had called synchronized method will be owner. 'synchronized (obj) {' If you mean this line of the code then its not a synchronized method but synchronized block. In this case its ThreadTest which will hold lock on obj which is instance of SecondThread.

3.what is mean by releasing lock here obj.wait(); means ?

Ans. Releasing a lock means giving up their claims allowing other threads to acquire lock on that object so that they can execute synchronized methods/blocks. obj.wait() here means that ThreadTest wants to give up the lock and wait until some one calls notify on 'obj' once other thread has called notify and this notification is received by ThreadTest it will try to get the lock on 'obj'

Once lock is released can same object come inside synchronized block and access code until previous thread get notified ?

Ans. Yes it can but provided that same object owns the lock for 'obj' as this is the requirement to execute any synchronized method/block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM