
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the use of Thread.sleep() method in Java?
The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.sleep() method must be enclosed within the try and catch blocks or it must be specified with throws clause. Whenever we call the Thread.sleep() method, it can interact with the thread scheduler to put the current thread to a waiting state for a specific period of time. Once the waiting time is over, the thread changes from waiting state to runnable state.
Syntax
public static void sleep(long milliseconds) public static void sleep(long milliseconds, int nanoseconds)
The sleep(long milliseconds) method makes a thread to sleep for some specific milliseconds only.
The sleep(long milliseconds, int nanoseconds) method makes a thread to sleep for some specific milliseconds plus nanoseconds.
Example
class UserThread extends Thread { public void run() { for(int i=1; i <= 5; i++) { System.out.println("User Thread"); try { Thread.sleep(1000); // sleep/stop a thread for 1 second } catch(InterruptedException e) { System.out.println("An Excetion occured: " + e); } } } } public class SleepMethodTest { public static void main(String args[]) { UserThread ut = new UserThread(); ut.start(); // to start a thread } }
Output
User Thread User Thread User Thread User Thread User Thread
- Related Articles
- C# Program to implement Sleep Method Of Thread
- How to use isAlive() method of Thread class in Java?
- How to use the sleep method in C#?
- Java Concurrency – sleep() method
- What is the use of setBounds() method in Java?
- The join() method of Thread Class in Java
- What method is used to create a daemon thread in Java?
- What is the Thread class in Java?
- What is the use of the toEpochSecond() method in Java 9?
- isAlive() method of Thread Class in Java programming
- What is the use of the Optional.stream() method in Java 9?\n
- Differences between wait() and sleep() method in Java?
- Difference Between sleep() and wait() Method in Java
- What is a daemon thread in Java?
- What is the JavaScript version of sleep()?
