线程的状态:
- 新建(New)
- 可运行(Runnable)
- 正在运行(Running)
- 阻塞(Blocked)
- 终止(Dead)
线程的生命周期
sleep方法应用
Thread类的方法
public static void sleep(long millis)
作用:在指定的毫秒数内让正在执行的线程休眠(暂停执行)
参数为休眠的时间,单位是毫秒
Java
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 30; i++) {
System.out.println(Thread.currentThread().getName() + "执行第" + i + "次!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class SleepDemo {
public static void main(String[] args) {
MyThread mt = new MyThread();
Thread t = new Thread(mt);
t.start();
Thread t1 = new Thread(mt);
t1.start();
}
}