Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deadlock K&B program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deadlock K&B program" Watch "Deadlock K&B program" New topic
Author

Deadlock K&B program

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
K&B book deadlock program is below



it says "Assume that read() is started by one thread and write() is started by another thread.if there are two different thrads thay may read and write independently,there is a risk of deadlock at line 8 or 16.

I tried to execute the above program with two thread.but i am having trouble how to start two new threads calling read() and write() methods.please help me on this.

my program is
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Put the calls to read and write in run method.


To be doubly sure have a sleep method between the synchronizations

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks for the answer swarna.


so without creating an anonymous inner class,i don't think this program running in two different threads calling read() and write() method is possible.Am i correct?
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Objective - one thread needs to run read and the other write.
This can be done by the following:-
1) Anonymous class
2) Non static Inner class
3) Static Nested class
4) Create 2 subclasses A1 and A2 extending Chapter9, one calling read and the other calling write in run method.

These are the only ways i think we can run the required scenario.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deadlock K&B program