首页 > 代码库 > java 线程sleep方法

java 线程sleep方法

public static void main(String[] args) {
  Runner1 r1 = new Runner1();
  Thread t = new Thread(r1);
  t.start();
  try {
   Thread.sleep(10000);
  } catch (InterruptedException e) {
   
  }
  t.interrupt();
  System.out.println("子线程结束");
 }

 

class Runner1 implements Runnable{
 boolean flag = true;
 @Override
 public void run() {
  while(flag){
   System.out.println("---"+new Date()+"---");
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    flag = false;
   }
  }
 }
 
}

java 线程sleep方法