/** * Created by li on 2016/11/24. */public class DemoThread extends Thread { private boolean stopThread = false; public void stopDemoThread() { this.stopThread = true; } @Override public void run() { while (!interrupted()) { if(!stopThread){ System.out.println("DemoThread关闭"); break; } System.out.println("bulabulabula"); } }}
千千万万不要用stop()方法,这个方法会瞬间解除所有锁并且停止线程。