File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Reentrant lock fairness parameter in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reentrant lock fairness parameter in constructor" Watch "Reentrant lock fairness parameter in constructor" New topic
Author

Reentrant lock fairness parameter in constructor

Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

Hi,
Having this description from this http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html javadoc. I am little puzzled by the following bit:


The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation. Note however, that fairness of locks does not guarantee fairness of thread scheduling. Thus, one of many threads using a fair lock may obtain it multiple times in succession while other active threads are not progressing and not currently holding the lock.


What's the relation between fairness of locks and fairness of threads?
When set true, under contention, locks favor granting access to the longest-waiting thread.
.
It means that it just suggests a kind of scheduling, however at the end of it it's the system who decides right?

Thanks in advance.



OCAJP 7, OCPJP 7
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Nick Widelec wrote:
What's the relation between fairness of locks and fairness of threads?
When set true, under contention, locks favor granting access to the longest-waiting thread.
.
It means that it just suggests a kind of scheduling, however at the end of it it's the system who decides right?



Scheduling applies to runnable threads. The scheduler chooses the next runnable thread to execute -- based on priority, time since last run, etc. And yes, all current JVM implementations do use the native scheduler to schedule the thread, and hence, it is the system that does the "fairness of threads".

Lock fairness applies to blocked threads -- specifically, the threads that are blocked on the lock. It is the lock that decides which thread to moved into the runnable state, and hence, can be scheduled. The other threads are still blocked, and hence, can't be scheduled.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reentrant lock fairness parameter in constructor
 
Similar Threads
Logical/File Locking and nested synchronisation
Synchronized methods and blocks
ReentrantReadWriteLock understanding
Atomic Variables vs Synchronization