在多个线程上使用 Thread.join 出现 NullPointer 异常
我的 Java 程序之一遇到以下问题。 我正在尝试根据主程序在文件系统上找到的内容来启动多个线程。 它的工作方式非常经典: - 第一个循环:实例化、存储在本地数组中并启动一个新线程 - 第二个循环:使用“.join()”方法等待所有线程
在执行时,我在“.join()”上收到 NullPointerException。此异常是由启动的第三个线程引发的(因为它在第 2 个线程之前完成)
这是我的代码示例:
PackageManager[] myRootManagers = new PackageManager[myCCDirs.size()];
int i = 0;
for (PkgDescriptor descriptor : myCCDirs) {
...
// --- instantiate new ROOT manager
myRootManagers[i] = new PackageManager(getConfig(), loggerPac, descriptor);
// --- start it
myRootManagers[i].start();
}
for (PackageManager packageManager : myRootManagers) {
try {
packageManager.join();
}
catch (InterruptedException e) {
loggerPac.error("...");
}
}
有人知道为什么会发生此异常吗?
I have the following issue with one of my Java programs.
I'm trying to launch several threads depending on what my main program find on the file system.
The way it works is quite classic:
- First loop: instantiate, store in a local array and start a new thread
- Second loop: wait all of thread with '.join()' method
At execution i get a NullPointerException on the '.join()'. This exception is thrown by the 3rd thread launched (because it finishes before the 2 first one)
This is an example of my code:
PackageManager[] myRootManagers = new PackageManager[myCCDirs.size()];
int i = 0;
for (PkgDescriptor descriptor : myCCDirs) {
...
// --- instantiate new ROOT manager
myRootManagers[i] = new PackageManager(getConfig(), loggerPac, descriptor);
// --- start it
myRootManagers[i].start();
}
for (PackageManager packageManager : myRootManagers) {
try {
packageManager.join();
}
catch (InterruptedException e) {
loggerPac.error("...");
}
}
Does someone know why this exception occurs ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保第一个循环中的所有
i
覆盖myRootManagers
数组中的所有有效不确定因素。请注意,您应该在第一个 for 循环的 end 中增加
i
,因为数组 indecies 是从 0 开始的。:作为调试步骤,我会
在第一个循环之后 添加,断言不存在
null
引用。Make sure all
i
in the first loop covers all valid indecies in themyRootManagers
array.Note that you should increment
i
in the end of the first for loop, since array indecies are 0-based.:As a debugging step, I would add
after the first loop, to assert that there are no
null
references left.这对我来说似乎很奇怪,除非你隐藏了重要的部分:
你迭代
myCCDirs
但初始化myRootManagers
对象,也许你没有增加i
?This seems odd to me unless you hidden the important part:
You iterate over
myCCDirs
but initializemyRootManagers
objects, maybe you didn't incrementi
?如果在另一个线程中删除 myCCDirs,也可能会发生这种情况。
This could also happen if a myCCDirs is removed in another thread.