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()
{
}
}
浙公网安备 33010602011771号