标准的停止进程步骤

package com.cyj.thread.stop;

public class Mystop {

	public static void main(String[] args) {
		Running r = new Running();
		new Thread(r).start();
		
		//外部干涉
		for(int i=0; i<=250; i++) {
			if(250 == i) { //改变标识
			  r.stop(); //此stop方法是重写的,原来默认的方法已过时
			}
			System.out.println("不正经地输出");
		}	
	}
}

class Running implements Runnable{
	//线程类中,定义线程体的使用的标识
	private boolean flag = true;
	
	public void run() {
		
		//线程体使用该标识
		while(flag) {
			System.out.println("很认真地输出");
		}
	}
	
	public void stop() {
		this.flag = false;
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42036616/article/details/81064818