线程休眠指的是让线程执行速度稍微慢一点。休眠方法:
·public static void sleep(long time)throws InterruptedException
范例:
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
}
public class Demo {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt, "线程A").start();
new Thread(mt).start();
}
}
