一种可能的解决方案是调用Java3dThread.finish()
Java3D 线程上的方法。缺点是必须绕过 java 访问规则才能调用此方法,因为它是包私有的。这段代码对我来说很有效:
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 的实例。
现在我称之为dispose()
终止应用程序时终止 Java3D 的方法:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(final WindowEvent e) {
plater.dispose();
}
});
Where 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()
调用,但以这种方式使用反射有些脆弱。)