wait (), notify, notifyAll () using

wait (), notify, notifyAll () using

Reference: https://www.jianshu.com/p/25e243850bd2?appinstall=0

A), the lock java object model

The JVM is using an object inside a lock (synchronized) to maintain two sets, Entry Set and Wait Set, namely lock pool and wait for the pool.

Two), Entry Set: *

A If the thread already holds the object lock, at this time if there are other threads also want to get the object lock, then it can only enter the Entry Set, and is in the BLOCKED state of the thread.

You may enter Entey Set thread:

1).两个抢夺cpu的线程未抢夺到的一方。

2).notify()/notifyAll()唤醒却未抢夺到cpu的线程。

三)、Wait Set:

If thread A calls the wait () method, the thread A releases the lock of the object, enter the Wait Set, and in the WAITING state of the thread.

May thread into the wait state:

1).调用wait()方法。

D), change Runnable state

Entry Set the thread state is Blocked state:

1).当对象锁被释放的时候,JVM会唤醒处于Entry Set中的某一个线程,这个线     程的状态就从BLOCKED转变为RUNNABLE。

Wait Set the thread state is Waiting state:

1)  .当对象的notify()方法被调用时,JVM会唤醒处于Wait Set中的某一个线程,这个线程的状态就从WAITING转变为RUNNABLE。

2).当notifyAll()方法被调用时,Wait Set中的全部线程会转变为RUNNABLE状态。所有Wait Set中被唤醒的线程会被转移到Entry Set中。

Note: Only Runnable state in order to compete thread lock, access to resources.

Distinguish five), notify () and notifyAll () of

notify () wakes up only one thread, notifyAll () wakes up all the threads Wati Set.

Vi), using the notify () prone to deadlock condition

For example: the producer and consumer threads

A consumer to consume, to determine buff is empty, call the wait () to enter the wait state, consumers consume two buff is empty, call the wait (), enter the wait state, a producer for production, full of buff, notify () Consumer one, this time to snatch two producers in the Entry Set the resource to determine the full buff, into the wait state, a consumer resource consumption, notify (), if awakened producers continue to produce at this time if a producer exit production, awakened consumers two, buffer is empty, enter the wait state consumers two, this time, the two producers and consumers into a wait state, no state Runable threads, producers and consumers 2 2 wait Set waiting for each other, a deadlock occurs.

Note: with reference to the specific code https://www.jianshu.com/p/25e243850bd2?appinstall=0

Seven), wait (), notify, notifyAll () to use with synchonized

Explain why here with synchonized:

1).如果线程要调用对象的wait()方法,必须首先获得该对象的监视器锁,调用
 wait()之后当前线程又立即释放掉锁,线程随后进入WAIT_SET(等待池)中。

2).如果线程要调用对象的notify()/notifyAll()方法,也必须先获得对象的    监视器锁调用方法之后,立即释放掉锁然后处于Wait_set的线程被转移到      Entry_set(等锁 池)中去竞争锁资源.。The Winner Thread,也就是    成功获得了对象的锁的线程,就是对象锁的拥有者,会进入runnable状态。

3).由于需要获得锁之后才能够调用wait()/notify()方法,因此必须将它们放    到同步代码块中.

Eight), summary

1) .Jvm internal lock object (synchonized) maintains two collections Entry Set and Wait Set.

2) Not to seize cpu or failed to seize the wake resources to the thread cpu resources will be placed in the Entry Set.

3) Calling wait () method of the thread into the Wait Set.

4) .Entry thread Set collection for Blocked state, Wait Set collection thread Waiting state.

5). When the lock is released resources, one thread state Entry Set will become Runnable state.

6). When you call notify () method, Wait Set of a thread to be awakened by waiting Runable state to state, calling notifyAll (), all threads Wait Set have been awakened state to a waiting thread Runable state and into Entry Set in.

Guess you like

Origin www.cnblogs.com/Auge/p/11712564.html