Question: ( c ) The class Book, listed below, is thread safe but may deadlock. Explain why and re - write it so that deadlocks cannot

(c) The class Book, listed below, is thread safe but may deadlock. Explain why and re-write it
so that deadlocks cannot occur.
class Book{
private String title;
private Lock lock = new ReentrantLock();
Book(String t){title = t;}
String title(){
lock.lock();try{ return title;}finally{lock.unlock();}
}
void set(String nt){
lock.lock();
try{title = nt;}finally{lock.unlock();}
}
public boolean equals(Object p){
lock.lock();
try{
Book b =(Book)p;
b.lock.lock();
try{ return(title.equals(b.title));}
finally{b.lock.unlock();}
}finally{lock.unlock();}
}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!