Multithreading - The waiting and notification

wait/notify

Notify the waiting thread and the thread is synchronized two types of threads on the same subject

Use process

// Wait
 synchronized (object) {
     // the protection condition is not satisfied, the current thread is suspended, waiting to enter the set while ( protection conditions!) {Object .wait ();} doSomething ();} // wake synchronized (object) { / / update the shared variable thread waiting conditions involve protection of updateSharedState (); Object .notify ();}

Internally

Java virtual machine for each object maintains a set of threads EntrySet entrance application for storing objects inside the lock, and wait for the set WaitSet waiting thread on the storage objects;

  • Thread B to acquire the lock

  • Protection condition is not satisfied, thread B is suspended, and releases the lock (atomic operation), the reference current thread into which the object belongs wait concentrated

  • A thread lock application

  • A thread acquires the lock

  • A thread update protection conditions

  • Protection conditions are met, wake any thread waiting concentrate

  • Thread B wakes up but remain concentrated in wait, try to acquire the lock successfully acquired lock, remove the thread wait set B, Object.wait () call returns

// wait pseudocode 
Pulic void the wait () {
     IF ( the Thread .holdLock ( the this)) { the throw new new IllegalMonitorStateException ();} IF (waitSet .Contains ( the Thread .currentThread ())) {addToWaitSet ( the Thread .currentThread () );} // {RELEASELOCK atomic atomic ( the this); // wait to pause the current thread wake-Block ( the thread .currentThread ());} // wakes up after acquireLock ( the this); removeFromWaitSet ( the thread .currentThread ()); return;}

Example: Monitoring service / wait timeout achieve control /Thread.join

wait / notify question

  • problem

    wait (long) can not distinguish whether the problem is due to return to wait for a timeout caused

  • solve

    awaitUtil Condition classes of a package juc (Date) method (implemented TODO)

Wake up early

  • problem

    Since an object only to maintain a wait set, when a protection condition A in this object is updated and set up, will call notifyAll wakes up all waiting threads, then use the thread waiting for other protective condition B of A will be awakened, He found himself protection conditions are not satisfied, can only continue to wait.

  • solve

    await method under Condition class packet juc

Loss of signal

  • problem

    Protection conditions are true judgment call and not wait on a loop in the critical area (todo)

  • problem

    notify wake up without consideration of any protection conditions, ie without success notify waiting threads wake up on the current wait condition may wake up other threads or a protective conditions did not wake up, to be replaced notifyAll

Deceptive wake

  • problem

    In case there is no waiting thread notify / notifyAll depending on the operating system led to wake up, leading to premature wake, the need to protect the conditions established in the judgment and wait to call a cycle in the critical area

Resulting in more context switches

  • the reason

    • Application and release the lock
    • Waiting thread wakes up from suspend to
    • Runnable threads and apply a lock for the corresponding internal lock
    • Waiting thread to wake up too early
  • solve

    • Under the premise of ensuring the correctness of programs, notify instead of notifyAll
    • Notification thread executing the lock is released as soon as possible after notify / notifyAll, resulting in multiple applications need to be awakened thread lock has been suspended (todo)

notify / notifyAll selection

  advantage Shortcoming Be applicable
notify   Loss of signal Polymorphs waiting thread
notifyAll Correctness Wake up early  

Specific case

Thread.join () is a synchronous method, its interior is the use of wait / notify to achieve, and then wait for the end of the target thread execution to continue; waiting threads call wait when the target is not the end of the thread to suspend its own, the virtual machine in the target thread after the run notifyAll execution method run. (Concept of synchronous what todo)

Condition

A wait / notify alternatives, await a method similar to wait, signal a method similar to notify, signalAll similar notifyAll; lock created by the display Lock, and request an explicit thread holds the lock Condition created instance. Can be used to solve the wait (long) can not distinguish because of a timeout or wait to be notified to return, and wake up too early problems. Condition instance also called a condition variable, internal memory maintains a queue of waiting threads.

Use process

class ConditionTest{
    private final Lock lock = new ReentrantLock(); private final Condition condition = lock.newCondition(); public void awaitFunc() throws Exception{ lock.lock(); try{ while(!保护条件){ condition.await(); } doSomething(); }finally{ lock.unlock(); } } public void signalFunc(){ lock.lock(); try{ updateState(); condition.signal(); }finally{ lock.unlock(); } } }

The principle

Establishing correspondence between the protective conditions were variable, so that the waiting thread to protect different conditions call the corresponding condition variable await methods to achieve wait, signal empathy.

Guess you like

Origin www.cnblogs.com/hangzhi/p/11279522.html