java如何停止一个wait的线程
1个回答
展开全部
java.lang.System类
static void exit(int status)
终止当前正在运行的 Java 虚拟机。
追问
可以用interrupt写一个结束线程的例子吗,写的详细点吧。。。。
追答
public class Demo extends Thread {
volatile private boolean stop = false;
public void setStop(boolean stop) {
this.stop = stop;
}
public Demo(ThreadGroup group, String name) {
super(group, name);
}
public static void main(String args[]) throws Exception {
Demo thread = null;
ThreadGroup tg = new ThreadGroup("洒家的线程组");
for (int i = 0; i < 5; i++) {
thread = new Demo(tg, "thread-" + i);
thread.start();
}
Thread.sleep(3000);
System.out.println("去吧皮卡丘,中断他们");
Thread[] ts = new Thread[tg.activeCount()];
tg.enumerate(ts);
for (Thread t : ts) {
((Demo)t).setStop(true);
t.interrupt();
}
Thread.sleep(1000);
System.out.println("都特么挂了");
}
public void run() {
String threadName = Thread.currentThread().getName();
synchronized (this) {
System.out.println(threadName + " 跑起来了");
while (!stop) {
try {
System.out.println(threadName + " wait了");
wait();
} catch (InterruptedException e) {
System.out.println(threadName + " 不再wait了");
}
}
}
System.out.println(threadName + "拜拜");
}
}
仅供参考
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询