gpt4 book ai didi

java - 网络浏览器中的 IllegalThreadStateException(Windows)

转载 作者:行者123 更新时间:2023-12-01 05:01:27 25 4
gpt4 key购买 nike

我收到 IllegalThreadStateException。

在 Eclipse 的调试器下一切正常,但当我尝试在 Firefox 上测试它时,它开始失败。第一次运行正常,刷新网页后出现异常。

这是代码:

public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
createConnection();
}
});
}
catch (Exception e)
{
System.err.println("createGUI didn't complete successfully");
e.printStackTrace();
System.err.println(e.toString());
}
}


private void createConnection()
{
_connectionThread = ConnectionThread.getInstance();
_connectionThread.setServer(getCodeBase().getHost());
_connectionThread.start(); <------ Exception
}

我是 Java 新手,老实说,这是我的第一个小程序。我知道我做错了什么,但我找不到它是什么。

编辑:从控制台

java.lang.reflect.InvocationTargetException
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
at com.onlinegame.gameclient.GameClient.init(GameClient.java:76)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at com.onlinegame.gameclient.GameClient.createConnection(GameClient.java:175)
at com.onlinegame.gameclient.GameClient.access$100(GameClient.java:34)
at com.onlinegame.gameclient.GameClient$1.run(GameClient.java:81)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.reflect.InvocationTargetException

最佳答案

 _connectionThread = ConnectionThread.getInstance();

看起来 ConnectionThread 是一个单例(这是真的吗?)并且扩展了 Thread。如果这是正确的,您可能会遇到问题,因为线程只能启动一次( Thread.start() )。如果您尝试第二次启动同一个 Thread 实例,它将失败并抛出 java.lang.IllegalThreadStateException

要解决这个问题,请在每个请求上创建一个新的 Thread 对象,或者将执行结果缓存在单例中。如何准确实现取决于您想要通过线程实现什么以及应用程序的设计。

关于java - 网络浏览器中的 IllegalThreadStateException(Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13341982/

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