This question is full theoretical, I'm sorry but I cannot avoid this time.
I'm learning about ReentrantLock
and read this:
Note however, that fairness of locks does not guarantee fairness of thread scheduling.
What does this mean? How can I imagine this?
Let's suppose that the lock is not held by anyone right now:
t1
thread (who is not the longest waiting thread) t1
tries to acquire the lockt1
because t1
is not the longest waiting threadt1
goes to sleepDoes Java work this way? In a very unsuccesful case this would mean lots of context switching (that leads to poor throughput, that is written down in the documentation).
What does this mean?
The OS will schedule the thread to run whenever it likes.
How can I imagine this?
The OS has little idea what the JVM would like to run next.
Does Java work this way?
Yes, Java doesn't control the OS scheduler.