Photo by João Jesus from Pexels In Java, there is a class ReentrantLock that is used for implementing Fair lock. This class accepts optional parameter fairness. When fairness is set to true, the RenentrantLock will give access to the longest waiting thread. The most popular use of Fair lock is in avoiding thread starvation. Since longest waiting threads are always given priority in case of contention, no thread can starve. The downside of Fair lock is the low throughput of the program. Since low priority or slow threads are getting locks multiple times, it leads to slower execution of a program. The only exception to a Fair lock is tryLock() method of ReentrantLock. This method does not honor the value of the fairness parameter.
Comments
Post a Comment