package com.android.main;


public class Lock {
    private boolean using;
    private String lastUser;
    public Lock() {
        this.using = false;
        lastUser = "HB2";
    }
    public synchronized void wantToUse(String newUser){
    	//synchronized(this){
	        while(lastUser == newUser){
	            try {
	                wait();
	            } catch (InterruptedException ex) {}
	        }
    	//}
    }
    public synchronized void leave(String lastUse){
    	//synchronized(this){
	        lastUser = lastUse;
	        notifyAll();
    	//}
    }
    public synchronized void wantToUse(){
        while(this.using ){
            try {
                wait();
            } catch (InterruptedException ex) {}
        }
        this.using = true;
    }
    public synchronized void leave(){
        this.using = false;
        notify();
    }
}
