模拟打印当前时间

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestThreadSleep implements Runnable{
   private boolean flag=true;
   @Override
   public void run() {
       while (flag){

               int num =1;
               System.out.println("这是副线程第"+num+++"次出现");
               try {
                   Thread.sleep(10);
              } catch (InterruptedException e) {
                   e.printStackTrace();
              }

          }


  }
   public void stop(){
       this.flag=false;
       System.out.println("不让副线程跑了啦");
  }

   public static void main(String[] args) {
       TestThreadSleep testThreadSleep = new TestThreadSleep();
       new Thread(testThreadSleep).start();
       for (int i = 0; i < 1000; i++) {
           System.out.println("副线程跑的好慢哦");
           if (i==900){
               testThreadSleep.stop();
               break;
          }
      }

       Date date = new Date(System.currentTimeMillis());
       while (true){
           System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
           try {
               Thread.sleep(1000);

          } catch (InterruptedException e) {
               e.printStackTrace();
          }
           date=new Date(System.currentTimeMillis());
      }
  }
}

 

posted on 2021-03-07 16:19  要给小八赚罐头钱  阅读(69)  评论(0)    收藏  举报