public synchronized void lock(){
        while(locked){
            try{
                if(this.thread != Thread.currentThread())
                    wait();
                else
                    break;
            }catch(Exception e){}
        }
        
        this.thread = Thread.currentThread();
        locked = true;
    }