线程状态转换

sleep / join / yield方法
sleep方法
使得当前线程休眠(暂停停止执行millie毫秒).
由于是静态方法,sleep可以由类名直接调用:
Thread.sleep(...)
join方法
合并某个线程
yield方法

sleep / join / yield方法
sleep方法
可以调用Thread的静态方法
public static void sleep(long millis) throws InterruptedException(重写的方法不能抛出与原方法不同的异常)使得当前线程休眠(暂停停止执行millie毫秒).
由于是静态方法,sleep可以由类名直接调用:
Thread.sleep(...)
join方法
合并某个线程
yield方法
让出CPU,给其他线程执行的机会
代码示例:
// TestInterrupt.java
import java.util.*;
public class TestInterrupt {
public static void main(String args[]) {
MyThread thread = new MyThread();
thread.start();
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
}
thread.interrupt();
}
}
class MyThread extends Thread {
public void run() {
//使线程停下来的方法,定义一个flag = true;
while(true) {
System.out.println("===" + new Date() + "===");
try
{
sleep(1000);
}
catch (InterruptedException e)
{
return;
}
}
}
}