转自:http://www.java2000.net/p6943
- import java.util.*;
- public class T {
- static boolean running = true;
- public static void main(String[] agrs) {
- Thread a = new Thread(new Runnable() {
- public void run() {
- try {
- while (running) {
- Thread.sleep(1000);
- System.out.println("###" + new Date() + "###");
- }
- } catch (InterruptedException e) {
- return;
- }
- }
- });
- a.start();
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {}
- running = false; // 1 此方法使用标志进行,推荐使用
- // a.stop(); // 2 此方法已经不推荐使用了
- }
- }