在线程体中调用sleep()方法会使当前线程进入睡眠状态,调用时需要传入一个毫秒数作为当前线程睡眠的时间
public class ThreadSleepTest {
public static void main(String[] args) {
System.out.println("主线程开始执行");
Thread thread1=new Thread(new SleepRunner());
thread1.start();
System.out.println("启动一个线程一");
Thread thread2=new Thread(new NormalRunner());
thread2.start();
System.out.println("启动一个线程2");
}
}
class SleepRunner implements Runnable{
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<100;i++){
System.out.println("SleepRunner"+i);
}
}
}
class NormalRunner implements Runnable{
public void run() {
for(int i=0;i<100;i++){
System.out.println(" NormalRunner"+i);
}
}
}