Thread class contains the sleep()method. There are two overloaded methods of sleep() method present in Thread Class, one is with one argument and another one is with two arguments. The sleep() method is used to stop the execution of the current thread (whichever might be executing in the system) for a specific duration of time and after that time duration is over, the thread which is executing earlier starts to execute again.
Syntax
There are 2 variations of the sleep() method in Java Thread. These are:
public static void sleep(long millis)
public static void sleep(long millis, int nanos)
Parameters:
- millis: Duration of time in milliseconds for which thread will sleep
- nanos: This is the additional time in nanoseconds for which we want the thread to sleep. It ranges from 0 to 999999.
Exceptions:InterruptedException, IllegalArguementException
Important Points:
- Thread.sleep() pauses the current thread's execution.
- Throws InterruptedException if another thread interrupts during sleep.
- Actual sleep duration may vary based on system load; higher load increases sleep time.
We can use Thread.sleep() method for any thread, i.e., we can do it with the main thread or any other thread that we make programmatically.
Example 1: Using Thread.sleep() Methodfor Main Thread
Java// Sleeping the main threadimportjava.io.*;importjava.lang.Thread;classGeeks{publicstaticvoidmain(String[]args){// we use throws keyword followed by exception// name for throwing the exceptiontry{for(inti=0;i<5;i++){// sleep the main thread for 1 sec// for every loop runsThread.sleep(1000);// printing the value of the variableSystem.out.print(i+" ");}}catch(Exceptione){// catching the exceptionSystem.out.println(e);}}}Output:
0 1 2 3 4
Example 2:Using Thread.sleep() methodfor Custom thread
Java// Sleeping the Custom Threadimportjava.lang.Thread;// Class extending the Thread ClassclassMyThreadextendsThread{// Overriding the run method@Overridepublicvoidrun(){// use throws keyword followed by exception// name for throwing the exceptiontry{for(inti=0;i<5;i++){// method will sleep the threadThread.sleep(1000);// printing the value of the variableSystem.out.print(i+" ");}}catch(Exceptione){// catching the exceptionSystem.out.println(e);}}publicstaticvoidmain(String[]args){// created threadMyThreadobj=newMyThread();obj.start();}}Output:
0 1 2 3 4
Example 3:IllegalArguementExceptionwhen sleep time is Negative
Java// Showing how exception can occur if we// pass the negative timeout value.importjava.lang.Thread;classGeeks{publicstaticvoidmain(String[]args){// Use throws keyword followed by exception// name for throwing the exceptiontry{for(inti=0;i<5;i++){// this will throw the// IllegalArgumentExceptionThread.sleep(-100);// Printing the value of the variableSystem.out.println(i);}}catch(Exceptione){// Catching the exceptionSystem.out.println(e);}}}Outputjava.lang.IllegalArgumentException: timeout value is negative
- The sleep() method with one parameter is a native method, meaning its implementation is done in another programming language.
- The method with two parameters is not a native method; its implementation is done in Java.
- Both the sleep() methods are static, meaning they can be accessed using the Thread class.
- Both methods throw a checked exception, so you must handle the exception either using the throws keyword or within a try-catch block.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java