圣思园java se培训总结(99-)(线程停止的方式)

Thread类中有stop方法,但是不建议使用

常用的停止线程的方法是

package com.yuxi.lesson97;

public class ControlThread
{
	static MyThread myThread = new MyThread();

	public static void main(String[] args)
	{
		startThread();
		// 某个需要的时刻去调用下面的结束方法
		// stopThread();
	}

	private static void startThread()
	{
		new Thread(myThread).start();
	}

	private static void stopThread()
	{
		myThread.stopRun();
	}
}

class MyThread implements Runnable
{
	private boolean flag = true;

	@Override
	public void run()
	{
		while (flag)
		{
			doSomeThing();
		}
	}

	public void stopRun()
	{
		this.flag = false;
	}

	private void doSomeThing()
	{

	}

}
原文地址:https://www.cnblogs.com/yuxishua/p/5103441.html