Reputation: 4000
I understand that the sleeping time isn't precise or as precise as the OS could make it be.
Assuming no other extreme factors..
My question is what if a thread starts sleeping for say, 10 seconds, then a GC hits after 5 seconds (in the middle of the thread sleeping time) and continues for 10 seconds, will the thread end sleeping right away or will it sleep for more 5 seconds (overall 20 seconds) ?
Upvotes: 3
Views: 1286
Reputation: 120868
This depends on a few factors, but in general the answer has to be 15
, give or take (more give vs take). It depends on where the safe-point poll is when executing sleep
. In my mind a thread is already at a safepoint when it executes sleep
(and I see no reason to do it otherwise + it seems maaartinus has the same thought). You can read here why that would matter.
So your thread has slept for 5 seconds
, GC
kicks in and does a stop-the-world (AFAIK the GC thread "protects" the page and as such a "memory protection error" happens and this is how the VM does a safepoint poll), so the entire application stops for 10 seconds
, but wall clock time has still progressed. As such there is no point in sleeping 5
more seconds; it is easily determined that the thread has already slept enough.
Upvotes: 3
Reputation: 180418
My question is what if a thread starts sleeping for say, 10 seconds, then a GC hits after 5 seconds (in the middle of the thread sleeping time) and continues for 10 seconds, will the thread end sleeping right away or will it sleep for more 5 seconds (overall 20 seconds) ?
I suppose you're asking about a stop-the-world GC, which is not the only or most common kind. All non-GC threads are ineligible to run during such a GC, but sleep()
time does not (directly) take that into account.
You can think of sleep()
as making the invoking thread ineligible to run until a specific future time. It will typically resume very soon after that time arrives, but if something else, such as a then-running GC, prevents it from doing so then its resumption will be delayed until after that situation is resolved. To put it another way, GC time can overlap sleep time, but the two are separate and independent factors that each (temporarily) prevents a thread from progressing.
Upvotes: 2