Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java blocked threads take up more CPU resources?

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.

like image 706
Oh Chin Boon Avatar asked May 05 '11 10:05

Oh Chin Boon


People also ask

Does blocked thread consume CPU?

A thread is inactive when in the blocked or waiting state. When in these states, the thread does not consume any CPU cycles. A thread is in the waiting state when it wants to wait on a signal from another thread before proceeding. Once this signal is received, it becomes runnable.

Does Java thread sleep consume CPU?

To be more clear: Threads consume no CPU at all while in not runnable state. Not even a tiny bit. This does not depend on implementation.

What causes high CPU in Java?

High CPU in java applications are due to huge volume of incoming requests or some intensive task performed within the code.

Does multithreading reduce CPU usage?

Although you can take advantage of multithreading to perform several tasks simultaneously and increase the application's throughput, it should be used judiciously. Incorrect usage of multithreading may result in high CPU usages or increased CPU cycles and can drastically reduce your application's performance.


1 Answers

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.

like image 157
Peter Lawrey Avatar answered Oct 23 '22 04:10

Peter Lawrey