multithreading - Java java.util.concurrent.locks.Condition interface documentation -
Inside the document for the condition interface:
is an example class that uses the condition . I will copy / paste here:
Class BoundedBuffer {Last lock lock = new ReentrantLock (); Final bet not full = lock. NewCondition (); Not the last bet = empty = lock. New condition (); Last item [] item = new object [100]; Int petter, techtor, count; Public Zero Insert (Object x) Throws Interwerted Explanation {lock.lock (); Try {while (count == items.length) not full.await (); Item [putptr] = x; If (++ putptr == items.length) putptr = 0; ++ count; notEmpty.signal (); } Finally {lock.unlock (); }} Public object () throws blocking exception {lock.lock (); Try {while (count == 0) notEmpty.await (); Object x = item [techter]; If (++ techter == item.label) left = 0; --count; NotFull.signal (); Return x; } Finally {lock.unlock (); }}} I do not understand how this might possibly work (note: I did not test it, I think it works) a deadlock Without making?
If I call Bunda Befter a hundred times on deal time, then it will not be completely closed. Waiting () method and it will be under lock If I do BoundedBuffer.take () from a thread then I should be locked on the first line in Lock ().
Am I missing something here?
Yes you do not remember anything from:
a condition The key provider providing is that it releases the lock indirectly and suspends the current thread, such as the object. White.
Therefore, the lock is issued when await () is called. wait () the method itself by java code void await () throws the blocker exposure
to the current thread It does not wait until the signal is blocked or interrupted. The lock associated with this condition has been released atomically and the current thread thread is disabled for scheduling purposes and remains inactive until it is one of the four:
And the method sign () awakens a thread that will retrieve the lock before continuing. signal from JavaDoc of the method : zero signal ()
Awaiting a wait thread if any thread is waiting on this condition So one is chosen for waking. The lock should be restored before returning to that thread.
Comments
Post a Comment