threadstop.java

来自「java课件 java课件 java课件 java课件」· Java 代码 · 共 38 行

JAVA
38
字号
import java.util.*;

class Timer implements Runnable{
	boolean flag=true;
	public void run(){
	  while(flag){
	    System.out.print("\r\t"+new Date()+"...");
	    try{
	    	Thread.sleep(1000);
	    }catch(InterruptedException e){}	
	  }
	  
	  System.out.println("\n"+Thread.currentThread().getName()+" Stop");
		
	}
	
	public void stopRun(){
		flag = false;
	}
}

public class ThreadStop{
	public static void main(String args[]){
	   Timer timer = new Timer();
	   Thread thread = new Thread(timer);	
	   thread.setName("Timer");
	   thread.start();
	   for(int i=0;i<100;i++){
	   	 System.out.print("\r"+i);
	   	 try{
	    	Thread.sleep(100);
	    }catch(InterruptedException e){}	
	   }
	   
	   timer.stopRun();
	}
}

⌨️ 快捷键说明

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