2

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

Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
  • Do you mean the `readLock()` and `writeLock()` methods? These don't block at all. Are you asking whehter threads use CPU while waiting to obtain the lock? – Mike Daniels Mar 15 '11 at 21:06
  • Sorry, yes, those are the methods I am talking about. I was looking at the reference variables in my code. The Java api states, "If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the write lock has been acquired, at which time the write lock hold count is set to one." So I guess my question is, does "dormant" mean no CPU cycles? It sounds like it, but I hate to assume anything where Threads are concerned. Thanks. :) – Swifty McSwifterton Mar 15 '11 at 21:14

1 Answers1

5

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • (Fixed the topic reflect the correct method names.) Yes I guess my question is if two threads both attempt to get the lock, does the one which is rendered dormant (according to the API) consume any cpu cycles? – Swifty McSwifterton Mar 15 '11 at 21:22
  • Stopping and starting a thread is relatively expensive, but this cost is not ongoing and once dormant, the thread doesn't consume cycles. By relatively expensive a mean about 10-20 micro-seconds. That might not sound like much but it could be 50,000 clock cycles. – Peter Lawrey Mar 15 '11 at 21:27
  • 1
    Peter, thanks very much. That clarifies things a a great deal. – Swifty McSwifterton Mar 15 '11 at 21:39