• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deadlocks and the scheduler

 
priti manas duddilla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deadlocks happen when there are two objects having synchronized blocks and two different threads trying to run the synchronized blocks while each one is holding onto a lock that the other needs.
Well, why can't a scheduler make sure that a thread won't go to sleep while it enters a synchronized block/method. In other words, why does a scheduler kick a thread away from running state while it is holding a key. Why can't it make sure that the thread releases a lock before it can be kicked out from running state or is put to sleep ?

Could that be a solution to fix potential deadlocks ?

Thanks,
Priti
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
priti manas duddilla wrote:
Well, why can't a scheduler make sure that a thread won't go to sleep while it enters a synchronized block/method. In other words, why does a scheduler kick a thread away from running state while it is holding a key.


Not sure what you mean by this. Whether a thread goes to sleep() or not is part of the code it is running. Are you suggesting that the sleep() method should no longer work when a lock is held?

Also, the only two states actively handled by the scheduler are running and runnable. Thread that are sleeping or blocked (waiting for a resource) are doing so because it is running code that requires it to be so. You can't actively run code when the current code is doing stuff like "waiting for the IO to complete from file". There is nothing to run.

Henry
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
priti manas duddilla wrote:Why can't it make sure that the thread releases a lock before it can be kicked out from running state or is put to sleep ?

Could that be a solution to fix potential deadlocks ?


Well, that would definitely fix potential deadlocks. It will also make the locking mechanism useless to protect against any race condition related to anything that can block -- such as anything that uses the disk, uses the network, or even uses other locks.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!