java concurrent programming of ReentrantLock (a)

package com.wangan.logistic;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class WanganReentrantLockService {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    
    private void await() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "线程获得了lock");
            System.out.println (Thread.currentThread () getName (). 
            Lock.lock ();+ "Thread await, temporarily hand over the lock and into the waiting queue" ); 
            Condition.await (); 
            System.out.println (Thread.currentThread (). GetName () before + "thread wakes up from await, obtain surrender lock and continue " ); 
        } the catch (InterruptedException E) { 
            e.printStackTrace (); 
        } the finally { 
            lock.unlock (); 
            . System.out.println (Thread.currentThread () getName () +" thread release Lock " ); 
        } 
    } 
    
    Private  void Signal () {
         the try { 
            . System.out.println (Thread.currentThread () getName () +" thread obtained Lock " );
            condition.signal (); 
            System.out.println (Thread.currentThread () getName (). + "thread issues signal, unfair wakeup queue waiting for other threads" ); 
        } the catch (Exception E) { 
            e.printStackTrace ( ); 
        } the finally { 
            lock.unlock (); 
            System.out.println (Thread.currentThread () getName (). + "thread releases the lock" ); 
        } 
    } 
    
    
    public  static  void main (String [] args) { 
        WanganReentrantLockService-Service = new new WanganReentrantLockService ();
         new new the Thread ( new newThe Runnable () { 
            @Override 
            public  void RUN () {
                 / * 
                 * this can simulate what would happen if the thread runs slower than the main thread, what would happen 
                    the try { 
                        the Thread.sleep (5000); 
                    } the catch (InterruptedException E) { 
                        E .printStackTrace (); 
                    } 
                * / 
                service.await (); 
            } 
        .}) Start (); 
        the try { 
            the Thread.sleep ( 3000);     // prevented from running faster than the main thread thread-0 faster 
        } the catch (InterruptedException E) {
            e.printStackTrace();
        }
        service.signal();
    }
}

Console output:

The Thread- 0 threads gained Lock 
the Thread - 0 thread await, temporarily hand over the lock and enter the waiting queue 
main threads gained Lock 
main thread issues signal, Unfair wake up other threads waiting in the queue 
main thread releases the lock 
the Thread - 0 thread He was awakened from await surrender before acquiring the lock and proceed to 
the thread -0 thread releases the lock

Program process is finished, the process ends normally.

If the code is commented out part of the release, deliberately out of the new thread running slower than the main thread, then the console output:

main thread acquires the Lock 
main thread issues signal, Unfair wake other threads waiting in the queue 
main thread releases the lock 
the Thread - 0 thread obtained Lock 
the Thread -0 thread await, temporarily hand over the lock and into the waiting queue

Since this time last thread has been in a Thread-0 await no other thread to wake up, the program would have been blocked in the process.

 

Finally, look at the instructions lock.newCondition () is:

Condition java.util.concurrent.locks.Lock.newCondition()

Returns a new Condition instance that is bound to this Lock instance.
Before waiting on the condition the lock must be held by thecurrent thread.
A call to Condition.await() will atomically release the lockbefore waiting
and re-acquire the lock before the wait returns.

newCondition () returns a Condition instance with the current lock binding. The current thread must obtain a lock, to condition.await ().

Once called condition.await (), the current thread releases the lock and then enter the waiting (this time lock other threads will be obtained), and will re-apply lock (thread holding the lock signal to inform other threads wait before returning from wait to re-apply lock, then the thread holding the lock releases the lock).

 

Guess you like

Origin www.cnblogs.com/lyhero11/p/12219160.html
Recommended