publicclassTestStopimplementsRunnable{privateboolean flag =true;@Overridepublicvoidrun(){int i =0;while(flag){
System.out.println("run...."+i++);}}publicvoidstop(){this.flag =false;}publicstaticvoidmain(String[] args){
TestStop testStop =newTestStop();newThread(testStop).start();for(int i =0; i <1000; i++){
System.out.println("main----"+i);if(i==900){
testStop.stop();
System.out.println("线程停止了!");}}}}
publicclassTestSleep2{publicstaticvoidtenDemo()throws InterruptedException {int num =10;while(true){
Thread.sleep(1000);
System.out.println(num--);if(num==0){break;}}}}
package PrimeThread.Demo02;import java.text.SimpleDateFormat;import java.util.Date;publicclassTestSleep2{publicstaticvoidmain(String[] args){
Date date =newDate(System.currentTimeMillis());//获取系统当前时间while(true){try{
Thread.sleep(1000);
System.out.println(newSimpleDateFormat("HH:mm:ss").format(date));//更改当前时间
date =newDate(System.currentTimeMillis());}catch(InterruptedException e){
e.printStackTrace();}}}}
线程状态线程停止public class TestStop implements Runnable{ private boolean flag = true; @Override public void run() { int i = 0; while (flag){ System.out.println("run...."+i++); } } public void stop(){