Movatterモバイル変換


[0]ホーム

URL:


Open In App

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:

Exceptions:InterruptedException, IllegalArguementException

Important Points:

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);}}}

Output
java.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.

Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp