
class Bticket extends Thread { Object train,comp; Bticket(Object train,Object comp){ this.train=train; this.comp=comp; } public void run(){ synchronized(train){ System.out.println("bookticket locked on train"); try{ Thread.sleep(200);} catch(InterruptedException ie){} } System.out.println("bookticket now waiting to lock on comp"); synchronized(comp){ System.out.println("bookticket locked on comp");
} } } class Cticket extends Thread
{ Object train,comp; Cticket(Object train,Object comp){ this.train=train; this.comp=comp; } public void run(){ synchronized(train){ System.out.println("cancellticket locked on train"); try{ Thread.sleep(200);} catch(InterruptedException ie){} } System.out.println("cancellticket now waiting to lock on comp"); synchronized(comp){ System.out.println("cancelticket locked on comp");
} }
}
class Deadlock1 { public static void main(String[] args) { Object tr = new Object(); Object compartment =new Object(); Bticket bt=new Bticket(tr,compartment); Cticket ct = new Cticket(tr,compartment); Thread t1=new Thread(bt); Thread t2 = new Thread(ct); t1.start(); t2.start(); } }
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.