【发布时间】:2019-03-26 07:45:27
【问题描述】:
这是我在社区的第一篇文章。欢迎任何cmets和建议。 我在端点http://ip:port/report 上有一个网络服务。该服务执行一个 Runnable 类。执行 Runnable 后,是否可以通过单独的服务调用来检查/监视线程?
我仍在工作并寻找 ExecutorService 之类的东西,但仍然没有运气。有没有其他方法可以做到这一点?请提出一些我可以检查的可能的解决方案。对不起我的语法。我希望我能用下面的示例代码解释清楚。
我的代码是这样的。
在http://ip:port/report上运行线程
public String runReport {
// Run the the runnable
Report report = new Report();
String threadName = "REPORT1";
Thread t = new Thread(report, threadName);
t.start();
// return thread some details
return t.getId() + "|" + t.hashCode();
}
我的可运行类
public class Report {
private String status;
@Override
public void run() {
//Update status
setStatus("Running");
//... do stuff
//Update status
setStatus("End")
}
// Getter and Setter
}
我在http://ip:port/report/check/some_param上的跳棋课
public String check( int threadId ) {
// Search the thread by threadId and check the current status
//Report.getStatus();
}
【问题讨论】:
-
Thread.currentThread()? -
显然我不能使用当前线程进行检查,因为它在不同的调用中。我需要的是获取我在调用期间传递的具有特定 ID 的线程,然后检查该线程的状态。
-
您需要维护一个
Map<Long, Thread>,将线程ID映射到线程。但要注意:一旦线程终止,id 可能会被重用!不过,可能还有其他(更好的)解决方案。但是这个问题有点太宽泛了。 -
谢谢@Seelenvirtuose,现在我从你关于维护地图的回答中得到了一个新的想法。我还可以使用数据库中的表格来获取报告详细信息。但我仍在寻找一些方法来做到这一点。
标签: java multithreading