The condition structure provides the following basic methods:
void await () throws interruptedexception; void awaituninterruptibly (); long awaitnanos (long nanostimeout) throws interruptedexception; boolean await (long time, Timeunit unit) Span style= "COLOR: #0000ff" >throws interruptedexception; boolean awaituntil (Date deadline) throws Span style= "COLOR: #000000" > interruptedexception; void signal (); void signalall ();
- The await () method causes the current thread to wait while releasing the current lock, and when the Singal () and Signalall () methods are used in other threads, the thread will regain the lock and continue execution. Or when the front-line is interrupted, jump out of the queue.
- The awaituninterruptibly () method is basically the same as the await () method, but it does not interrupt the response during the wait.
- The Singal () method is used to wake up a waiting thread,
Public classReenterlockconditionImplementsRunnable { Public StaticReentrantlock lock =NewReentrantlock (); Public StaticCondition Condition =lock.newcondition (); /*** When an object implementing interface <code>Runnable</code> was used * To create a thread, start ing the thread causes the object ' s * <code>run</code> method to being called in that separately executing * Thread. * <p> * The general contract of the method <code>run</code> are that it could take any action wha Tsoever. * * @seeThread#run ()*/@Override Public voidrun () {Try{lock.lock (); Condition.await (); System.out.println ("Thread is going on"); } Catch(interruptedexception e) {e.printstacktrace (); } finally{lock.unlock (); } } Public Static voidMain (string[] args)throwsinterruptedexception {reenterlockcondition R1=Newreenterlockcondition (); Thread T1=NewThread (R1); T1.start (); Thread.Sleep (2000); Lock.lock (); Condition.signal (); Lock.unlock (); }}
When the thread uses condition.await (), the thread is required to hold the associated re-entry lock, and after the condition.await () call, the thread will release the lock, and in the same way that the Condition.signal () method call will also require the line enters upgradeable to obtain the associated lock. After the signal () method call, the West Wing wakes up a thread from the current wait queue of the condition object. Once the thread is awakened, he tries again to get the re-entry lock that is bound to it, and once it is successfully fetched, it can continue, so after the signal () method call, It is generally necessary to release the associated lock. The implementation of put () is as follows:
Public voidPut (e e)throwsinterruptedexception {if(E = =NULL)Throw NewNullPointerException (); //Note:convention in all put/take/etc are to preset local var//holding count negative to indicate failure unless set. intc =-1; Node<E> node =NewNode<e>(e); FinalReentrantlock Putlock = This. Putlock; FinalAtomicinteger count = This. Count; Putlock.lockinterruptibly ();//can respond to an interrupted lock and lock Try { /** Note that count was used in wait guard even though it was * not protected by lock. This works because count can * is decrease at the this point (all other puts is shut * out by Lock), and we (or some other waiting put) is * signalled if it ever changes from capacity. similarly * for all and uses of count in other wait guards. */ while(Count.get () = = capacity) {//determine if the queue is fullNotfull.await ();//wait} enqueue (node);//Inserting Datac = count.getandincrement ();//total number of updates, variable C is the value before count plus 1 if(C + 1 <capacity) notfull.signal ();//have enough space to notify other threads}finally{putlock.unlock ();//Release Lock } if(c = = 0) Signalnotempty ();//after the insert succeeds, notify the take operation to go to the data}
by Takelock and Putlock two lock, Linkedblockingqueue realizes the separation of data and write data, so that both of them can be operated concurrently in real sense.
- Lock coarsening, in the development process, it should be consciously in a reasonable situation of the locking of the coarse, especially in the loop request for lock, if a lock on a continuous request, synchronization and release, itself will also consume valuable resources, but not for performance optimization,
Performance optimization is based on the actual situation of the runtime of the various resource points trade-offs between the process, the idea of lock coarsening and reduce the holding time of the lock is the opposite, but on different occasions, their effect is not the same. All of us need to weigh the situation.
Good partner for re-entry lock: Condition conditions (Reading notes)