线程中断
发布日期:2024-04-25 21:44:47 浏览次数:1 分类:技术文章

本文共 762 字,大约阅读时间需要 2 分钟。

线程中断:通过设置标记来中断线程,不要使用interrupt()方法

public class InterruptedDemo {

public static void main(String[] args) {
RunnableIn rin = new RunnableIn();
Thread thread = new Thread(rin);
thread.start();
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i==10) {
rin.setFlag(false);  //修改标记
}
System.out.println(Thread.currentThread().getName()+"正在听第"+(i)+"首歌!");
}
}
}
class RunnableIn implements Runnable{
int i = 0;
boolean flag = true;
public void setFlag(boolean flag){
this.flag = flag;
}
@Override
public void run() {
while(flag && i<20){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在下载第"+(i++)+"首歌!");
}
}
}

转载地址:https://blog.csdn.net/u013013433/article/details/52274480 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:线程中断机制
下一篇:线程中操作form控件

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月15日 09时40分18秒