Home » Java programming language

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

Java Thread Class static void sleep(long time_in_ms) method: Here, we are going to learn about the static void sleep(long time_in_ms) 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)

  • This method is available in package java.lang.Thread.sleep(long time_in_ms).
  • sleep(long time_in_ms) method is applicable when we want to stop current executing thread for a particular amount of time in milliseconds or other words if a thread causes the current thread to stop executing for some time in milliseconds given in the method.
  • This method is static so we can access this method with 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 compiletime error.
  • This method is overloaded.

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) method like this /* Thread.sleep(1000)*/ inside MyThread so this thread will stop its execution for 1000 millisecond and 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){
    }

Parameter(s):

When we write Thread.sleep(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds and 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 < 5; ++i) {
            System.out.println("Thread started:" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } 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 < 2; ++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 ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0 


Comments and Discussions!

Load comments ↻





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