import java.util.*; public class BusyWait implements Runnable { protected MyLock lock; BusyWait(MyLock l) { lock = l; } public static void main(String args[]) { MyLock sharedLock = new MyLock(); new Thread(new BusyWait(sharedLock)).start(); new Thread(new BusyWait(sharedLock)).start(); } public void run() { try { while(lock.isLocked() == true) { Thread.sleep(300); System.out.println(Thread.currentThread().getName()+" awaites locked for the lock."); } // Uncomment the following line and see threads running concurrently. // Thread.sleep(10); lock.lock(); for(int i=0; i<10; i++) { Thread.sleep(100); System.out.println(Thread.currentThread().getName()+" iteration: "+i); } lock.unlock(); System.out.println("Exiting!"); } catch(InterruptedException ex) { ex.printStackTrace(); } } }