• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

219
Vistas
How does unfair lock have better performance than fair lock?

Interface Lock has a useful boolean fairness parameter to guarantee fairness in locking - thread that waits for lock the longest, gets the lock first. And I thought I wanted to use it everywhere as it prevents starvation. Well, not before I read that it can cost us performance.

I couldn't find an answer this question, so I hope someone can clear this up. Meaning, what is the difference between taking threads that "honor" fairness and threads that don't? Aren't they both stored in some "ordinary" queue where other waiting threads are?

over 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

When a lock is released, and when more than one thread is waiting for it, the thread that's been waiting the longest is the one that's most likely to find that memory pages that it's going want have been swapped out while it was "sleeping." The thread that's been sleeping for the least amount of time is the most likely to be "ready-to-go."


Apologies to @xingbin if that's what you were trying to say.

over 3 years ago · Santiago Trujillo Denunciar

0

First of all it's not the interface Lock that has that fairness, but it's ReentrantLock.

The threads that issue Lock::lock might not be put into a queue at all, be that in fair or unfair mode.

The unfair mode is slightly easier to understand. In this mode, the thread that requests the lock (via calling Lock::lock), tries to get it immediately without getting enqueued. If that succeeds - done. It is not put into any queue, since it could get the lock. Notice that this does not care if there are already waiting threads in the queue, thus it is "unfair" (to the other already waiting threads). If it could not acquire the lock (meaning someone else has it), it is put on the queue.

On the other hand, in fair mode, the thread that calls lock, first has to check if there are already threads waiting for this lock. If there are no such threads and it can take the lock: no enqueue and take the lock. If it can't - it is enqueued.

One last point is that in both fair and unfair mode, the threads are put on the same queue; that is: fair/unfair is not about the internal representation of the queue.

over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda