【发布时间】:2015-04-13 11:53:32
【问题描述】:
按照这里的答案:How to know if other threads have finished?
我设法为这样的线程构建了一个侦听器:
NotifyingThread prcoessCatRegThread = new processCatFromPage(baseURL, 0, null);
prcoessCatRegThread.addListener(this);
prcoessCatRegThread.start();
@Override
public void notifyOfThreadComplete(NotifyingThread notifyingThread) {
// TODO Auto-generated method stub
System.out.println("Thread calledback returned.");
System.out.println(notifyingThread);
}
还有“processCatFromPage”类:
class processCatFromPage extends NotifyingThread{
int fromPage = 0;
Integer toPage = 0;
int cat = 0;
String baseURL = null;
static List<Posts> Posts = new ArrayList<Posts>();
private void processPage(int pageNum){
}
processCatFromPage(String baseURL, int fromPage, Integer toPage){
}
@Override
public void doRun() {
processPage(fromPage);
}
}
现在我尝试从“notifyOfThreadComplete”中的“processCatFromPage”类访问“Posts”,在eclipse中调试时我可以看到变量,但我不知道如何访问它..
notifyingThread.Posts
出现错误..所以我尝试了任何其他方法..
有什么帮助吗?
非常感谢!:)
我尝试通过代码访问的变量: http://i.stack.imgur.com/dWwgY.png
【问题讨论】:
-
你为什么不使用
ExecutorService? -
这能回答您的问题吗? help.eclipse.org/luna/…。如果没有,那么请解释你在哪里输入“notifyingThread.Posts”......让它给你错误。
-
@StephenC 我在变量视图窗口中看到变量,就像您附加的链接中一样,但无法在代码中访问它,“notifyingThread.Posts”是我尝试键入时的示例:public void notifyOfThreadComplete(NotifyingThread notifyingThread) { // TODO 自动生成的方法 stub System.out.println("线程回调返回。"); System.out.println(notifyingThread.Posts); }
-
@fge 监听器更适合我需要的目的,谢谢!
-
错误信息到底是什么?
标签: java multithreading listener