线程中终止循环的小办法




	public class ThreadText {
	    public static void main(String arg[]) {
	        d1 r = new d1();
	        r.start();
//	        Thread td = new Thread(r);
//	        td.start();
	        try {
				Thread.sleep(10000);
				  r.flag=false;//10s后终止循环
			} catch (InterruptedException e) {
				// TODO Auto-g enerated catch block
				e.printStackTrace();
			}
	      
	        
	    }
	    }
	class d1 extends Thread{
		boolean flag=true;
		int i=0;
	    public void run(){
	    	
	       while(flag){
	    	   
	    	   i++;
	            System.out.println(i);
	            try {
					sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }

	    }
	}

	


 

猜你喜欢

转载自blog.csdn.net/qq_42183409/article/details/88934543