Fede Rossi
Fede Rossi

Reputation: 69

CPU consumption of Thread.sleep()

Watching in the Sampler Tab / CPU, under the CPU Samples sub-tab, i see that the most consuming method is java.lang.Thread.sleepnative.

I have about 30 Threads with relative sleep() calls. But what's the meaning of this?

My average reported CPU consumption is 7%, is the Thread.sleep() really using the 70% of this time?

How is it possible, is it a "real" consumption?

Upvotes: 0

Views: 2197

Answers (1)

Simone Gianni
Simone Gianni

Reputation: 11672

Usually a sampler will simply check every given time what your program is doing, and report you how many times (and by a simple multiplication give an approximation of how much time) your program was inside a given method.

This often has nothing to do with real CPU load.

For example, a sleep() call pauses the thread, and as such the CPU is almost not loaded at all by your code, but still, if I have a program that only starts, sleeps for 10 minutes and ends, sampling it will tell me that Thread.sleep consumed 100% of my cpu time.

The same applies to IO operations, in which your thread is sitting waiting for data to arrive from somewhere (could be a remote internet host, or simply your disk), and is not significantly loading the CPU, but still the sampler will (correctly) tell you that most of the time has been spent inside IO methods, even if CPU itself was not doing that much.

Upvotes: 8

Related Questions