package Exam;

public class LockDemo extends Thread {
	private Lock lock_;
	private String name_;
	
	public static void demo() {
		System.out.println("\n\nStarting LockDemo.demo()");
		Lock sharedLock = new Lock();
		new LockDemo("A", sharedLock).start();
		new LockDemo("B", sharedLock).start();
	}
	
	public LockDemo (String name, Lock lock) {
		name_ = name;
		lock_ = lock;
	}
	
	public void run () {
		print("Entering CS");
		lock_.acquire();
		print("Computing");
		randomSleep();
		lock_.release();
		print("Leaving CS");
	}
	
	protected void print(String s) {
		System.out.println(name_ + ": " + s);
	}

	protected void randomSleep() {
		try {
			Thread.sleep((int)(Math.random() * 1000));
			// Thread.sleep((int)(5000));
		} catch (InterruptedException e) { }
	}
}