java线程通知 [英] java threads notify

查看:84
本文介绍了java线程通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个班级,fe Class1和Class2

I have 2 classes, fe Class1 and Class2

假设我在Class1中这样做:

let's say i do this in Class1:

Class2 class2 = new Class2();
Thread thread = new Thread(class2);
thread.start();
...
thread.stop();

现在我想在线程停止时检入Class2的run方法,该怎么办? 因此,当class1停止线程时,我想向class2发出警报,告知它已停止,然后做一些事情

now I want to check in the run method of Class2 when my thread stops, How can I do this? So when class1 stops the thread, I want to get some alert to class2 that it stopped and then do some things

推荐答案

  1. 不要使用stop(),请使用interrupt().

如果遵循点1,则在Class2中,您可以使用:

If you obey point 1, in Class2 you can use:


public void run() {
  if(Thread.currentThread().isInterrupted())
    //somebody interrupted me, ouch!
}

这篇关于java线程通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆