java 线程睡眠唤醒_Java睡眠线程

Java中的Thread.sleep()方法用于让当前线程暂停执行指定毫秒数,期间可能被其他线程中断,产生InterruptedException。线程休眠后,将在等待时间结束后变为可运行状态,等待CPU继续执行。示例展示了如何使用sleep()方法使线程休眠1500毫秒。
摘要由CSDN通过智能技术生成

java 线程睡眠唤醒

To sleep a thread for a specified time, Java provides sleep method which is defined in Thread class. The sleep method is an overloaded method which are given below. It throws interrupted exception so make sure to provide proper handler.

为了使线程Hibernate指定的时间,Java提供了Thread类中定义的sleep方法。 睡眠方法是一种重载方法,如下所示。 它会引发中断的异常,因此请确保提供适当的处理程序。

It always pause the current thread execution. Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.

它总是暂停当前线程的执行。 任何其他线程都可以在睡眠中中断当前线程,在这种情况下会抛出InterruptedException。

句法 (Syntax)

sleep(long millis)throws InterruptedException
sleep(long millis, int nanos)throws InterruptedException

示例:Hibernate线程 (Example : Sleeping a thread)

In this example, we are sleeping threads by using sleep method. Each thread will sleep for 1500 milliseconds and then resume its execution. See the below example.

在此示例中,我们通过使用sleep方法来使线程处于睡眠状态。 每个线程将Hibernate1500毫秒,然后恢复执行。 请参见以下示例。

public class MyThread extends Thread
{ 
        MyThread(String str){
            super(str);
        }
    
        public void run()
        {
                System.out.println(Thread.currentThread().getName()+" Started");
                try{
                    MyThread.sleep(1500); 
                    }catch(InterruptedException ie){
                        System.out.println(ie);
                }
                System.out.println(Thread.currentThread().getName()+" Finished");
        }
        public static void main(String[] args)
        {
                MyThread t1=new MyThread("first thread");
                MyThread t2=new MyThread("second thread");
                t1.start();
                t2.start();
        }
}

first thread Started second thread Started first thread Finished second thread Finished

第一个线程已启动第二个线程已启动第一个线程已完成第二个线程已完成

(Example)

public class MyThread extends Thread
{ 
        MyThread(String str){
            super(str);
        }
    
        public void run()
        {
                System.out.println(Thread.currentThread().getName()+" Started");
                try{
                    MyThread.sleep(1500);
                    System.out.println(Thread.currentThread().getName()+" Sleeping..");
                    }catch(InterruptedException ie){
                        System.out.println(ie);
                }
                System.out.println(Thread.currentThread().getName()+" Finished");
        }
        public static void main(String[] args)
        {
                MyThread t1=new MyThread("first thread");
                MyThread t2=new MyThread("second thread");
                System.out.println(t1.getName()+" state: "+t1.getState());
                t1.start();
                System.out.println(t1.getName()+" state: "+t1.getState());
                System.out.println(t2.getName()+" state: "+t2.getState());
                t2.start();
                System.out.println(t2.getName()+" state: "+t2.getState());
        }
}

first thread state: NEW first thread state: RUNNABLE second thread state: NEW first thread Started second thread state: RUNNABLE second thread Started first thread Sleeping.. second thread Sleeping.. second thread Finished first thread Finished

第一线程状态:NEW第一线程状态:RUNNABLE第二线程状态:NEW第一线程已启动第二线程状态:RUNNABLE第二线程已启动第一线程正在Hibernate..第二线程正在Hibernate..第二线程已完成第一线程已完成

Thread.sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution. So the actual time that current thread sleep depends on the thread scheduler that is part of operating system.

Thread.sleep()与线程调度程序进行交互,以在指定的时间段内将当前线程置于等待状态。 等待时间结束后,线程状态将更改为可运行状态,并等待CPU进一步执行。 因此,当前线程睡眠的实际时间取决于操作系统中的线程调度程序。

翻译自: https://www.studytonight.com/java/sleeping-thread-in-java.php

java 线程睡眠唤醒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值