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 线程睡眠唤醒