17

i would like to ask if Java will utilize more CPU resources when threads are blocked, i.e. waiting to lock a monitor which is currently being locked by another thread.

I am now looking at a thread dump whereby some threads are blocked as they are waiting to lock a monitor, and i am unsure if that is what may be accountable for the high CPU usage.

Thanks!

EDIT (6 May 2011) I forgot to mention if this behavior is relevant for Java SE 1.4.2.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

4 Answers4

32

Threads consume resources such as memory. A blocking/unblocking thread incurs a once off cost. If a thread blocking/unblocks tens of thousands of times per second this can waste significant amounts of CPU.

However once a thread is blocked, it doesn't matter how long it is blocked for, there is no ongoing cost.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Going by the answer posted by @sjlee, I came to know that if spinlock is involved with the blocked threads then this end up causing CPU utilization. Hence, don't you think that saying the blocked threads to be cost-less **always** is bit **strong** assumption ? – Vishal K Aug 25 '16 at 08:02
  • Please pardon me but I would really love to know your opinion on my previous comment. Or, any link or pointer to this topic would be great ! – Vishal K Aug 30 '16 at 05:11
  • 1
    @VishalK Blocked thread cost less if they block for a relatively long time. e.g. tens of milli-seconds. For very short period of time, blocking can have a high overhead. – Peter Lawrey Aug 30 '16 at 11:52
  • Thanks Peter for your valuable input. So, does it mean that for very short duration blocked thread, JVM involves Spinlock mechanism ? Is there any article which can provide me deep insight on this topic on JVM level? – Vishal K Aug 31 '16 at 03:56
  • @VishalK I would look at the source code for ReentrantLock which is documented. – Peter Lawrey Aug 31 '16 at 09:24
  • Thanks Peter. I would look at ReentrantLock source code. Your help is much appreciated. :) – Vishal K Sep 02 '16 at 05:21
  • What about those in WAITING/TIMED_WAITING state? – Diego Ramos Jul 22 '19 at 03:25
20

The answer is not so simple. There may be cases where threads that go into the blocked state may end up causing CPU utilization.

Most JVMs employ tiered locking algorithms. The often involve algorithms such as spinlocks especially for locks held for a short duration. When a thread tries to acquire a monitor and finds it cannot, the JVM may actually put it in a loop and have the thread attempt to acquire the monitor, rather than context switching it out immediately. If the thread fails to acquire the lock after a certain number of tries or duration (depending on the specific JVM implementation), the JVM switches to a "fat lock" or "inflated lock" mode where it does context switch out the thread.

It is with the spinlock behavior where you may incur CPU costs. If you have code that holds lock for a very short duration and the contention is high, then you may see appreciable bump in the CPU utilization. For some discussions on various techniques JVMs use to reduce costs on contention, see http://www.ibm.com/developerworks/java/library/j-jtp10185/index.html.

sjlee
  • 7,726
  • 2
  • 29
  • 37
4

No, threads that are blocked on a monitor do not take up additional CPU time.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

Suspended or blocked thread do not consume any CPU time.

Umesh K
  • 13,436
  • 25
  • 87
  • 129