Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deadlock K&B program

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic