stop thread
程序员文章站
2022-04-01 15:48:09
...
class MyThread extends Thread{
private volatile boolean isStop = false;
@Override
public void run() {
int i = 0;
while(!isStop){
i++;
}
}
public void setStop(boolean stop){
this.isStop = stop;
}
}
public class Test {
public static void main(String[] args) throws IOException {
Test test = new Test();
MyThread thread = test.new MyThread();
thread.start();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
}
thread.interrupt();
}
class MyThread extends Thread{
@Override
public void run() {
int i = 0;
while(!isInterrupted() && i<Integer.MAX_VALUE){
System.out.println(i+" while循环");
i++;
}
}
}
}
下一篇: thread stop() 方法为啥过期
推荐阅读
-
Dont stop til you get enough PHP 输出缓存详解
-
MySQL Thread pool 操作过程
-
stop-and-remove-all-docker-containers-and-images
-
No Session found for current thread 咋解决?ehcache失效 咋解决?
-
python线程锁(thread)学习示例
-
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
-
Dont stop til you get enough php中取得文件的后缀名?
-
C#利用System.Threading.Thread.Sleep即时输出信息的详解
-
php守护进程带start/stop/restart/stat功能
-
Java使用Thread创建多线程并启动操作示例