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.