This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which thread will get the lock

 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

Let's consider a scenario. A thread is running the code blocks inside a synchronized method thus owns the lock of that object.now let say two other thread simultaneously try to get the lock but waits since there is already one thread is having it.Now the thread is done with the critical section and releases the lock.Which one of the two waiting thread will enter the critical section when both of the waiting thread is of same priority value.

Regards,
Arka
 
Tony Docherty
Bartender
Posts: 2952
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either of them.
AFAIK at the end of a synchronized block all threads waiting on that block are woken and (assuming they are all the same priority) whichever one acquires the monitor gets to execute the code.
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Either of them.
AFAIK at the end of a synchronized block all threads waiting on that block are woken and (assuming they are all the same priority) whichever one acquires the monitor gets to execute the code.


Agreed. And they don't even have to be at the same priority. Thread priority behaviors are handle by the underlying threading system (for most JVM implementations), and there are a few reasons why the OS threading system may choose to run a lower priority thread over a higher priority thread.

Henry
 
Diplav Srivastava
Greenhorn
Posts: 3
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two thread will be waiting for acquiring the lock.sinc both of them have equal priority so anyone of them can acquire.here is a point.if the priority is differet by a small value 1 then even in that case jvm does not ensure that the higher priority thrrad will run
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope I’m not too late, but welcome to the Ranch
 
Diplav Srivastava
Greenhorn
Posts: 3
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic