Which three statements are correct about thread’s sleep method?

Which three statements are correct about thread’s sleep method?

Which three statements are correct about thread’s sleep method?

A.
The sleep (long) method parameter defines a delay in milliseconds.

B.
The sloop (long) method parameter defines a delay in microseconds.

C.
A thread is guaranteed to continue execution after the exact amount of time defined in the sleep (long) parameter.

D.
A thread can continue execution before the amount of time defined in the sleep (long) parameter.

E.
A thread can continue execution after the amount of time defined in the sleep(long) parameter

F.
Only runtime exceptions are thrown by the sleep method.

G.
A thread loses all object monitors (lock flags) when calling the sleep method.

Explanation:
public static void sleep(long millis)
throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds(A, not B). The thread does not lose ownership of any monitors(not G).
Parameters:
millis – the length of time to sleep in milliseconds.
Throws:
InterruptedException – if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.



Leave a Reply 11

Your email address will not be published. Required fields are marked *


Kamil

Kamil

ADE
I think D is more preferable against C because of case InterruptedException occurance.

Kamil

Kamil

*occurrence

Boris

Boris

..agree..
Two overloaded versions of sleep are provided: one that specifies the sleep time to the millisecond and one that specifies the sleep time to the nanosecond. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS. Also, the sleep period can be terminated by interrupts.
you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.(from Oracle site, chapter “Pausing Execution with Sleep“)

Charlie

Charlie

ADE. The execution is not guaranteed, could be earlier or later.

Peter

Peter

A,D (InterruptedException),E

Tim

Tim

ADE. For G: “the thread does not lose ownership of any monitors”.

me

me

with threads nothing is guaranteed ADE

wojtek

wojtek

ADE
When a thread encounters a sleep call, it must go to sleep for at
least the specified number of milliseconds (unless it is interrupted before its wake-up
time, in which case, it immediately throws the InterruptedException).