Java Thread类的静态void sleep(long time_in_ms)方法,带示例

线程类静态无效睡眠(long time_in_ms) (Thread Class static void sleep(long time_in_ms))

  • This method is available in package java.lang.Thread.sleep(long time_in_ms).

    软件包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.

    sleep(long time_in_ms)方法适用于我们要在特定时间段内(以毫秒为单位)停止当前执行线程的情况,换句话说,如果某个线程导致当前线程在该方法中给出的毫秒级内停止执行一段时间,则该方法适用。

  • 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.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get compiletime error.

    该方法抛出一个InterruptedException异常,因此需要通过try-catch或throws来处理异常,否则我们将获得编译时错误。

  • This method is overloaded.

    此方法已重载。

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

例如,我们有两个线程[ t1 – MyThread],[ t2 – main],因此将看到会发生什么。

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.

假设线程t1是否执行,并且与此同时,如果我们在MyThread中调用类似于/ * Thread.sleep(1000)* /的sleep(1000)方法,那么该线程将在1000毫秒内停止执行,并等待处理器和如果线程再次分配处理器,则同一线程将继续执行。

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.

当我们编写Thread.sleep(2000)时 ,此行表示当前正在执行的线程将在2000毫秒内停止其执行,并且我们需要记住,同一线程将在调用sleep()方法的地方停止其执行。

Return value:

返回值:

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

此方法的返回类型为void ,它不返回任何内容。

Java程序,演示sleep()方法的示例 (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 


翻译自: https://www.includehelp.com/java/thread-class-static-void-sleep-long-time_in_ms-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值