Java threads - A few important notes between threads

 In Java, you can call the Object object by blending wait () method, and notify () method or the notifyAll () method to communicate between threads. Call wait () in the thread method, the block waiting to inform other threads (other thread calls notify () method or the notifyAll () method), calling notify () method or the notifyAll () method in the thread, will notify other threads from wait () method returns at.

 

      Object is a superclass of all, it has five methods form the core of waiting / notification mechanism: notify (), notifyAll (), wait (), wait (long) and wait (long, int). In Java, all classes inherit from Object, therefore, all classes have these common methods are available. Moreover, since they are declared final, so any subclass can override a method.

 

     Here a few details about the various methods in use need to pay attention:

 

      1、wait()

      public final void wait()  throws InterruptedException,IllegalMonitorStateException

     The method used for the current thread into the hibernation state until notified or interrupted so far. Before calling wait (), the thread must obtain a lock on the object level object, that can only be called wait () method in the synchronization method or synchronized block. After entering the wait () method, the current thread releases the lock. Before () returns from wait, thread and other threads compete to regain lock. If you call wait (), does not hold the appropriate lock is thrown IllegalMonitorStateException, it is a subclass of RuntimeException, therefore, you do not need to try-catch structure.

 

     2、notify()

     public final native void notify() throws IllegalMonitorStateException

        This method also in the synchronization method or synchronized block calls, that before calling the thread must also obtain the object level lock of the object, without holding the appropriate lock If you call notify (), also throw IllegalMonitorStateException.

     The method used to notify other threads that may be waiting for this object lock. If there are multiple threads waiting, then any thread planner pick out a thread wait () to notify state and wait for it to acquire the object object lock (after notify, the current thread will not immediately release the object lock, where the thread does not wait to acquire the object lock immediately, to wait until after the program exits synchronized block of code, only the current thread releases the lock, the thread where the wait can also acquire the object lock), but does not disturb the other equally waiting to be objects notify thread them. When first get the object lock wait thread has finished running, it will free up the object lock, at this time if the object does not use notify statement again, even if the object has been idle, other threads to wait because there is no wait state to be informed of the object, it will continue blocking in wait until this object to issue a notify or notifyAll. It should be noted: they are waiting to be notify or notifyAll, rather than lock. This following notifyAll () method executes the different situations. 

 

     3、notifyAll()

     public final native void notifyAll() throws IllegalMonitorStateException

      This method works notify () method of the same, it is important differences:

      notifyAll wait for all of the original thread on the object of all exits wait state (ie all wake up, wait no longer notify or notifyAll, but since this time has not acquired the object lock, and therefore can not continue down), become waiting to acquire a lock on the object, once the object lock is released (notifyAll thread exits the synchronized code block calls notifyAll of the time), they will go to the competition. If one thread acquires the object lock, it will continue down, after it exits the synchronized code block, release the lock, the other has been awakened thread will continue to compete to acquire the lock, always continue until all the awakened thread is finished.

 

     4、wait(long)和wait(long,int)

     Obviously, these two methods are set to wait for the timeout, the latter, together with NS, accuracy is difficult to achieve on a time value, and therefore, this method is rarely used. For the former, if notified before the waiting thread or is interrupted, has exceeded the specified number of milliseconds, it regains lock through competition, and return from wait (long). In addition, we need to know, if you set the timeout when wait () returns, because we are not sure it is received notice due to timeout or return, because wait () method does not return any relevant information. But generally can be determined by setting the flag value of the flag bit is changed prior to notify, to read the value of the flag in the wait () method to determine, of course, not to be missed in order to ensure notify, we need an additional flag to determine whether the loop calls wait () method.

 

 

       In-depth understanding of:

   If a thread calls the object's wait () method, the thread will be the object in a wait pool, pool thread will not wait for the competition to lock the object.

   When there is a thread calls the object notifyAll () method (wake up all wait thread) or notify () method (random only wake a wait thread), wakes up the thread goes to lock the pool of the object, lock the pool thread lock to compete for the object.

   High priority threads competing to lock the probability of large objects, if a thread is no competition to the object lock, it will stay in the lock pool, the only thread calls again wait () method, it will return to the waiting pool in. The competition subject to lock down the thread execution continues until the execution is over synchronized block of code, it will free up the object lock, then lock the thread pool will continue to compete the object lock.

 


----------------
Disclaimer: This article is CSDN blogger "Lanting storm" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/ns_code/article/details/17225469

Guess you like

Origin www.cnblogs.com/zhilili/p/11971230.html