线程睡眠例子
public class ThreadTestsleep方法 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
try {
System.out.println(Thread.currentThread().getName()+"------->"+i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
中断睡眠例子
public class ThreadTest中断睡眠 {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThreadSleep());
t1.setName("t1");
t1.start();
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
class MyThreadSleep implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"begin");
try {
Thread.sleep(1000*60*60*24);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"end");
}
}