testinterrupt.java

来自「北京尚学堂J2SE所有的源代码!!北京尚学堂J2SE所有的源代码!!」· Java 代码 · 共 44 行

JAVA
44
字号
import java.util.*;
public class TestInterrupt {
  public static void main(String[] args) {
    MyThread thread = new MyThread();
    thread.start();
    try {Thread.sleep(10000);}
    catch (InterruptedException e) {}
    thread.interrupt();
  }
}

class MyThread extends Thread {
	boolean flag = true;
  public void run(){
    while(flag){
      System.out.println("==="+new Date()+"===");
      try {
        sleep(1000);
      } catch (InterruptedException e) {
        return;
      }
    }
  }
}
/*
public void run() {
    while (true) {
      String temp = new Date().toString();
      String t = temp.substring(11, temp.indexOf('C'));
      t = t.trim();
      String[] time = t.split(":");
      if (time.length == 3) {
        System.out.println(“现在是” + time[0] + “点” + 
                  time[1] + "分" + time[2] + "秒");
      }
      try {
        Thread.sleep(5000);
      } catch (InterruptedException e) {
        return;
      }  
    }
  }
}
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?