Sleep Time in Java
Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a for few seconds.
In java, it is considered as the sleeping time of the thread for which the time up to which the thread remains in sleeping stateand the thread again starts its execution from the position where it has stopped after the completion of sleeping time
The sleep() method is a thread method that is mainly used to pause the process for some seconds and go a thread in the waiting state andhis method does not return anything.
The Sleep() in java is a static method that is called from any contextand it followsthe synchronization.During the process of synchronization, the sleep() method doesn’t release the lock on the object andthe sleep() can be called from outside the Synchronised context.
This method takes sleeping time in milliseconds and here the information is not lost as it again starts the execution from where it stops and the sleep() method sends the current thread into the non-runnable state.
Syntax for Sleep Time Java
There are four variations in the sleep() method. The syntax for sleep time in java is given as:
Public static void sleep(long MLS) throws InterruptedException
Public static void sleep(long MLS) throws IllegalArguementException
Public static void sleep(long MLS, int n) throws InterruptedException
Public static void sleep(long MLS, int n) throws illegal argument exception
In java, the sleep time has the above four types of sleep() methods
Whereas the first two type takes only one argument and the second type takes two arguments
Parameters used in sleep time java or sleep() method are
- MLS
- n
mls= this is the first parameter and this MLS parameter represents the time in milliseconds and also it gives the duration at which the thread sleep by the sleep() method.
n = this is the second parameterthat is passed in the second sleep() method type.It is defined as the extra time up to which the user wants the thread to be sleeping by the sleep() methodand the run of n is from 0 to 999999.
We can understand these two sleep ()method types by seeing the below table
Public static void sleep(long MLS) throws InterruptedException Or Public static void sleep(long MLS) throws IllegalArguementException | Public static void sleep(long MLS,int n) throws InterruptedException Or Public static void sleep(long MLS,int n) throws IllegalArguementException |
It takes only one argument or one parameter It is a native method Here, we get the implementation of the native method by another programming language It throws a checked exception | It takes only twoarguments or twoparameters It is not a native method that is a non-native method Here, as we do not have a nativemethodwe get the implementation in java It also throws a checked exception |
Methods:
The sleep time in java has three types of methods; these methods are known as overloaded methods. They are given as
- wait()
- wait(long timeout, int nanoseconds)
- wait(long timeout)
Constructors:
The constructor for the sleep method is given as
Public static void sleep(long MLS) throws InterruptedException
Examples:
Example of the sleep() method on the custom thread
import java.io.*;
class TestSleepMethod1 extends Thread
{
public void run()
{
for(int i=1;i<4;i++)
{
try{Thread.sleep(300);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start();
}
}
output
1
1
2
2
3
3
Example of the sleep() method on the main thread
import java.lang.Thread;
import java.io.*;
public class TestSleepMethod2
{
public static void main(String argvs[])
{
try
{
for (int i = 0; i< 5; i++)
{
Thread.sleep(1000);
System.out.println(i);
}
}
catch (Exception ESPN)
{
System.out.println(expn);
}
}
}
output
0
1
2
3
An example of the sleep() methodis when the sleeping time is negative
import java.lang.Thread;
import java.io.*;
public class TestSleepMethod
{
public static void main(String argvs[])
{
try
{
for (int i = 0; i< 4; i++)
{
Thread.sleep(-400);
System.out.println(i);
}
}
catch (Exception ESPN)
{
System.out.println(expn);
}
}
}
Output
java.lang.IllegalArgumentException: timeout value is negative
Some important points
- it is a static method
- it is the concept of multithreading
- it is related to thread class
- it has 4 variations