gpt4 book ai didi

java - 使用 Java 7 Update 25 的 rmi 线程的 AppContext 为空

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:04 24 4
gpt4 key购买 nike

我们最近从 Update 21 更新到 Java 7 Update 25,现在当从 rmi 线程调用 SwingUtilities.isEventDispatchThread() 时遇到空指针异常,因为 AppContext.getAppContext() 返回 null。

java.lang.NullPointerException at sun.awt.SunToolkit.getSystemEventQueueImplPP(Unknown Source) at sun.awt.SunToolkit.getSystemEventQueueImplPP(Unknown Source) at sun.awt.SunToolkit.getSystemEventQueueImpl(Unknown Source) at java.awt.Toolkit.getEventQueue(Unknown Source) at java.awt.EventQueue.isDispatchThread(Unknown Source) at javax.swing.SwingUtilities.isEventDispatchThread(Unknown Source) at ... ... at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at sun.rmi.transport.Transport$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Unknown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

此错误仅在 Web 启动时出现,当我们通过 IDE 运行我们的应用程序时,它没问题。

还有其他人遇到过这个吗?知道有关 AppContext 的最新更新发生了什么变化吗?

似乎其他人在更新后遇到了与 AppContext 相关的问题: https://forums.oracle.com/message/11077767#11077767

最佳答案

我在 Java3D 作为 Java Web Start 运行时遇到了同样的问题。我找到了另一个解决方案。您必须准备自己的 InvokeLaterProcessor 和可运行队列。它必须扩展线程并获取可运行对象并在运行方法中处理它们:

public class InvokeLaterProcessor extends Thread {

private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<Runnable>(1);

public InvokeLaterProcessor(String name) {
super(name);
}

public void invokeLater(Runnable runnable) {
try {
queue.put(runnable);
} catch (InterruptedException ex) {
log.warn("invokeLater interrupted");
}
}

public void run() {
Runnable runnable=null;
do {
try {
runnable = queue.take();
SwingUtilities.invokeLater(runnable);
} catch (InterruptedException ex) {
runnable=null;
}
} while(runnable!=null);
}
}

你所要做的就是在主线程中创建的某个类的静态中创建它:

static {
invokeLaterProcessor=new InvokeLaterProcessor("MyInvokeLater");
invokeLaterProcessor.start();
}

并通过此代码处理可运行对象:

invokeLaterProcessor.invokeLater(runnable);

你不需要专有

sun.awt.SunToolkit.invokeLaterOnAppContext(evtContext, rn)

关于java - 使用 Java 7 Update 25 的 rmi 线程的 AppContext 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com