Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

deadlock or  RSS feed

 
sanjay dube
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Actually this is an interview question
I would like to know if say there are many threads trying to access a synchronized method of a object & say if thread 1 dies in the synchronized mehtod. What are the repercussions of this & what will be the solution.
Rgrds
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to define what you meant by "dies" (not a technical term!)
 
rak shar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
Even i had to face this qstn in 1 of my intrvws .
still wondering wat wil b the answer.
wel regarding die i reckon wat he wants to say if the thread gets detroyed or suspended
Rgrds
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if "destroyed" means destroy() is called, and "suspended" means suspend() is called, then the answers are rather different in those two cases; and there are other possible definitions of "died" as well, each with different meaning (stop(), wait(), sleep(), throwing a RuntimeException) If he wants an answer to one particular case, then I'll be glad to provide it; but if he wants to know the answers to all possible cases, then it probably makes more sense to read a book on the topic.
 
mahendra dubey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest
Here is the question put to me.
Say in a synchronised method i have teh database connection code
There arew many threads waiting to access this synchronised method.
Now the connection is obtained & say immediately the db goes down
Isnt this a deadlock scenario
How will you handle such a scenario
Rgrds
MD
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mahendra dubey:
Say in a synchronised method i have teh database connection code
There arew many threads waiting to access this synchronised method.
Now the connection is obtained & say immediately the db goes down
Isnt this a deadlock scenario
How will you handle such a scenario

First this not a deadlock scenario(this is not called a deadlock scenatio).This threads will wait indefinitely till the database is up or timeout occurs(if enabled).
Coming to handling such situations, you can check whether the db is up by putting some timeout for connection. When timeout expires, tell the user that db is down.
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sanjay dube:
I would like to know if say there are many threads trying to access a synchronized method of a object & say if thread 1 dies in the synchronized mehtod. What are the repercussions of this & what will be the solution.

Java uses monitors to thread synchronization. If one thread using the synchronized method gets destroyed,it will release the monitor key.This key can be used by other thread waiting. I don't think deadlock will occur in this situation.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Murthy Narasimha writes:
If one thread using the synchronized method gets destroyed,it will release the monitor key.

While this is essentially true in practice, it's not true in theory; I point this out because you use the word "destroyed". If you call destroy() on a Thread, in a JVM that actually implements destroy(), then the Thread will go away but any monitors it holds will not be released! Now, of course, Sun's JVMs, and therefore most if not all JVMs, don't actually implement destroy() -- they throw an UnsupportedOperationException if you call it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!