in Technology by
Explain Thread.sleep() in Java ?

1 Answer

0 votes
by

The Java Thread class provides the two variant of the sleep() method. First one accepts only an arguments, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known as the sleeping time of the thread. After the sleeping time is over, the thread starts its execution from where it has left.

The sleep() Method Syntax:

Following are the syntax of the sleep() method.

  1. public static void sleep(long mls) throws InterruptedException   
  2. public static void sleep(long mls, int n) throws InterruptedException   

The method sleep() with the one parameter is the native method, and the implementation of the native method is accomplished in another programming language. The other methods having the two parameters are not the native method. That is, its implementation is accomplished in Java. We can access the sleep() methods with the help of the Thread class, as the signature of the sleep() methods contain the static keyword. The native, as well as the non-native method, throw a checked Exception. Therefore, either try-catch block or the throws keyword can work here.

Related questions

0 votes
    What are the Parameters of Thread.Sleep() in Java?...
asked May 29, 2023 in Technology by JackTerrance
0 votes
    Which of these method is used to tell the calling thread to give up a monitor and go to sleep until ... division Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Thread priority in Java is? (a) Integer (b) Float (c) double (d) long The question was asked in ... in chapter Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which feature of java 8 enables us to create a work stealing thread pool using all available processors at ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Describe the purpose and working of sleep() method....
asked Jan 19, 2021 in Technology by JackTerrance
0 votes
    What is sleep mode in android?...
asked Jan 14, 2021 in Technology by JackTerrance
0 votes
    i am in hurry , i want to sleep early; but i have a test for tomorrow, i have not study what can ido Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    He could not sleep because (i) there was an animal outside (ii) he thought savages had com (iii) his bed was uncoinfortable Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    I am using simple thread to execute the httpGet to server when a button is clicked, but I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of the interface contains all the methods used for handling thread related operations in Java? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Thread priority in Java is? (a) Integer (b) Float (c) double (d) long The question was asked in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Which class provides thread safe implementation of List? (a) ArrayList (b) CopyOnWriteArrayList (c) HashList (d) ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    PriorityQueue is thread safe. (a) True (b) False The question was asked during an interview. My question is ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...