一种可能的解决方案是在 Java3D 线程上调用该方法。缺点是必须绕过java访问规则才能调用此方法,因为它是包私有的。这段代码为我做了伎俩:Java3dThread.finish()
public void dispose() {
virtualUniverse.removeAllLocales();
try {
// give the Java3D threads the chance to terminate peacefully.
Thread.sleep(250);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
// and now handle the threads that didn't take the hint
finishJava3dThreads();
}
private static void finishJava3dThreads() {
final Class<?> rendererClass;
final Method finishMethod;
try {
rendererClass = Class.forName("javax.media.j3d.J3dThread");
finishMethod = rendererClass.getDeclaredMethod("finish");
finishMethod.setAccessible(true);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
}
final ThreadGroup[] groups = new ThreadGroup[10];
final int count = Thread.currentThread().getThreadGroup().getParent().enumerate(groups);
for (int i = 0; i < count; i++) {
final ThreadGroup threadGroup = groups[i];
if ("Java3D".equals(threadGroup.getName())) {
threadGroup.setDaemon(true);
final Thread[] threads = new Thread[threadGroup.activeCount()];
final int threadCount = threadGroup.enumerate(threads);
for (int j = 0; j < threadCount; j++) {
final Thread thread = threads[j];
if (rendererClass.isInstance(thread)) {
try {
finishMethod.invoke(thread);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
Thread.yield();
threadGroup.interrupt();
}
}
}
其中 virtualUniverse 是之前在应用程序中创建的 VirtualUniverse 的实例。
现在我调用此方法在终止应用程序时终止 Java3D:dispose()
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(final WindowEvent e) {
plater.dispose();
}
});
其中 是包含上述方法的实例。plater
dispose()
其他所有内容都只是释放主 JFrame:
actions.put(EXIT_ACTION, new AbstractAction(EXIT_ACTION) {
@Override
public void actionPerformed(final ActionEvent e) {
dispose();
}
});
并且默认关闭操作也设置为:DISPOSE_ON_CLOSE
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
不过,不确定这是否是最佳选择。(我仍然更喜欢它而不是调用,但以这种方式使用反射有点脆弱。System.exit()