Code example for Lock

Methods: tryLock, unlock

0
package my.exercixe.concurr; 
 
import java.util.concurrent.TimeUnit; 
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 
 
public class AttemptLocking { 
	Lock lock = new ReentrantLock();
 
	public void untimed() { 
		boolean locked = lock.tryLock();
		try { 
			System.out.println("Locked " + locked);
		} finally { 
			if (locked)
				lock.unlock();
		} 
	} 
 
	public void timed() throws Exception { 
		boolean locked = false;
		try { 
			locked = lock.tryLock(2, TimeUnit.SECONDS);
			System.out.println("Locked " + locked);
		} catch (InterruptedException exc) {
			throw new Exception("Lock attempt interrupted.");
		} finally { 
			if (locked)
				lock.unlock();
		} 
 
	} 
 
	/**