Search code examples
javamultithreadingcpu-usagereentrantreadwritelock

Do dormant Threads made so by the readLock() or writeLock() methods in ReentrantReadWriteLock class consume CPU cycles?


I am using Java 6, and reading through Java Concurrency in Practice. I am trying to figure out if when using these methods, if a dormant thread waiting for the lock uses any CPU cycles while it is dormant. Anyone know? Thanks!

Matt


Solution

  • Any action consumes clock cycles. However when a thread is suspended, the number of clock cycles is fixed and does not depend on how long it is suspended for. This is good if the thread is suspended if even a relatively short period of times, but for a very short period of time its not very efficient which is why Lock doesn't suspend the thread immediately but retries a small number of times to get the lock before suspending the thread (in the hope it can avoid doing this)

    I assume you are talking about Lock.lock() which ReentrantReadWriteLock.readLock() and ReentrantReadWriteLock.writeLock() support.