java Lock study notes

The java.util.concurrent class library of java SE5 also contains an explicit mutual exclusion mechanism defined in java.util.concurrent.locks. The Lock object must be explicitly created, locked, and released.

The following rewrites EvenGenerator with an explicit Lock:

public class MutexEvenGenerator extends IntGenerator{
    
    
	private int currentEvenValue=0;
	private Lock lock=new ReentrantLock();
	@Override
	public int next() {
    
    
		// TODO Auto-generated method stub
		lock.lock();
		try {
    
    
			++currentEvenValue;
			Thread.yield();
			++currentEvenValue;
			return currentEvenValue;
		}finally {
    
    
			lock.unlock();
		}
	}
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        EvenChecker.test(new MutexEvenGenerator());
	}
}

MutexEvenGenerator adds a lock that is called mutually exclusive, and uses lock() and unlock() methods to create critical resources inside next(). The return statement must be placed in the try clause to ensure that unlock() does not occur prematurely, exposing the data to the second task .

public class AttempLocking {
    
    
    private ReentrantLock lock=new ReentrantLock();
    public void untimed() {
    
    
    	boolean captured=lock.tryLock();
    	try {
    
    
    		System.out.println("tryLock(): "+captured);
    	}finally {
    
    
    		if(captured) {
    
    
    			lock.unlock();
    		}
    	}
    }
    public void timed() {
    
    
    	boolean captured=false;
    	try {
    
    
    		captured=lock.tryLock(2,TimeUnit.SECONDS);
    	}catch(InterruptedException e) {
    
    
    		throw new RuntimeException(e);
    	}
    	try {
    
    
    	    System.out.println("tryLock(2,TIMEUNIT.SECONDS): "+captured);
    	}finally {
    
    
    		if(captured) {
    
    
    			lock.unlock();
    		}
    	}
    }
	public static void main(String[] args) throws InterruptedException {
    
    
		// TODO Auto-generated method stub
        final AttempLocking al=new AttempLocking();
        al.untimed();
        al.timed();
        new Thread() {
    
    
        	public void run() {
    
    
        		al.lock.lock();
        		System.out.println("aquired");
        	}
        }.start();
//        Thread.yield();
        Thread.sleep(1000);
        al.untimed();
        al.timed();
	}

}
/*
 *tryLock(): true
tryLock(2,TIMEUNIT.SECONDS): true
aquired
tryLock(): false
tryLock(2,TIMEUNIT.SECONDS): false
*/

ReentrantLock allows you to try to acquire but not acquire the lock , so if someone else has acquired the lock, then you can decide to leave to perform something else instead of waiting until the lock is released, as in the untime() method As seen in. An attempt is made to acquire the lock in time(), and the attempt can fail after 2 seconds.

The explicit Lock object also gives you more fine-grained control in terms of locking and releasing locks, compared to the built-in synchronized lock .

Guess you like

Origin blog.csdn.net/weixin_43916777/article/details/104265654