Code example for ReentrantLock

Methods: tryLock

0
package entrantLock; 
 
import java.util.concurrent.TimeUnit; 
import java.util.concurrent.locks.ReentrantLock; 
 
public class AttemptLocking { 
 
	private ReentrantLock lock=new ReentrantLock();
	public void untimed() 
	{ 
		boolean captured=lock.tryLock();
		try{ 
			System.out.println("tryLock():"+captured);
		}finally{ 
			if(captured)
				lock.lock();
		} 
	} 
	public void timed() 
	{ 
		boolean captured=false;
		try{ 
			captured=lock.tryLock(2, TimeUnit.SECONDS);
		}catch(InterruptedException e){
			throw new RuntimeException();
		}