Home » Java programming language

Java Thread Class static void sleep(long time_in_ms, int time_in_ns) method with Example

Java Thread Class static void sleep(long time_in_ms, int time_in_ns) method: Here, we are going to learn about the static void sleep(long time_in_ms, int time_in_ns) method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 30, 2019

Thread Class static void sleep(long time_in_ms, int time_in_ns)

  • This method is available in package java.lang.Thread.sleep(long time_in_ms, int time_in_ns).
  • sleep(long time_in_ms, int time_in_ns) method is applicable when we want to stop current executing thread for particular amount of time in milliseconds + nanoseconds (i.e. with additional time in nanoseconds) as well or in other words if a thread causes current thread to stop executing for some time in milliseconds + nanoseconds given in the method.
  • This method is static so we can access this method with the class name too.
  • The return type of this method is void so it does not return anything.
  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.
  • We pass here two parameters in the given method of the Thread class and the parameter will be time_in_ms(time in milliseconds) and time_in_ns(time in nanoseconds) this two-parameter is the duration of time to sleep of our Thread so this thread wait for ms+ns time.
  • If other thread takes less time to executes so in that case if we call sleep() method then there is a possibility of completion of the Thread because the current thread will wait for time_in_ms + time_in_ms.

For example, We have two threads [t1 – MyThread], [t2 – main] so will see what will happen.

Let suppose if a thread t1 executes and in the meanwhile if we call sleep(1000,500) method like this /* Thread.sleep(1000,500)*/ inside MyThread so this thread will stop its execution for 1000 millisecond and 500 nanoseconds will wait for the processor and if the thread allocates processor again then the same thread will continue its execution.

Syntax:

    static void sleep(long time_in_ms, int time_in_ns){
    }

Parameter(s):

When we write Thread.sleep(2000,1000) so this line means currently executing thread will stop its execution for 2000 milliseconds and additional 1000 nanoseconds we need to remember the same thread will stop its execution from where sleep() method is called.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of sleep() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class MyThread extends Thread {
    //Override run() method of Thread class 
    public void run() {
        for (int i = 0; i < 2; ++i) {
            System.out.println("Thread started:" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000, 500);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
        System.out.println("Thread Ended :" + Thread.currentThread().getName());
    }
}

class MainThread1 {
    public static void main(String[] args) throws Exception {

        MyThread mt = new MyThread();

        mt.start();

        for (int j = 0; j < 5; ++j)
            System.out.println("Thread started:" + Thread.currentThread().getName());
        System.out.println("Thread ended:" + Thread.currentThread().getName());
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread Ended :Thread-0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.