/*线程的休眠:Thread.sleep()*/
class MyThread6 implements Runnable{
public void run(){
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName()+"运行:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ThreadSleep {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread6 my6 = new MyThread6();
Thread th = new Thread(my6,"线程A");
th.start();
}
}
/*
运行结果:(每隔1s输出一个)
线程A运行:0
线程A运行:1
线程A运行:2
线程A运行:3
线程A运行:4
线程A运行:5
线程A运行:6
线程A运行:7
线程A运行:8
线程A运行:9
*/
class MyThread6 implements Runnable{
public void run(){
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName()+"运行:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ThreadSleep {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread6 my6 = new MyThread6();
Thread th = new Thread(my6,"线程A");
th.start();
}
}
/*
运行结果:(每隔1s输出一个)
线程A运行:0
线程A运行:1
线程A运行:2
线程A运行:3
线程A运行:4
线程A运行:5
线程A运行:6
线程A运行:7
线程A运行:8
线程A运行:9
*/

本文通过一个简单的Java程序示例介绍了如何使用Thread.sleep()方法实现线程的休眠功能,程序创建了一个名为“线程A”的线程并使其每秒输出一次当前循环变量的值。
214

被折叠的 条评论
为什么被折叠?



