java 线程停止

package com.prj;

public class TestStop implements Runnable {
	private boolean flag=true;
	public void run() {
		// TODO Auto-generated method stub
		int i=0;
		while(flag) {
			
			System.out.println("run..thread "+i++);
		}
	}
	
	public void stop() {
		// TODO Auto-generated method stub
		this.flag=false;

	}
	public static void main(String[] args) {
		TestStop teststop=new TestStop();
		new Thread(teststop).start();
		for (int i = 0; i < 100; i++) {
			System.out.println("main "+i);
			if(i==90) {
				teststop.stop();
				System.out.println("线程应该停止了");
			}
			
		}
		
		
	}

}

 

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页