【问题标题】:Illegal State Exception in javajava中的非法状态异常
【发布时间】:2017-10-24 13:19:48
【问题描述】:

我在运行代码时遇到了这个异常。即使代码运行良好,我在终端上也遇到了一些异常

Exception in thread "AWT-EventQueue-1" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:708)
    at SR.start(SR.java:38)
    at SR.mouseClicked(SR.java:212)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Window.processEvent(Window.java:2025)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我做错了什么??

我的start() 方法

private void start() throws IllegalStateException {
    if (HelperThread == null)
        HelperThread = new Thread(this);
    HelperThread.start();
}

【问题讨论】:

  • 那么,你试图启动一个已经完成的线程?
  • 我要附上我的代码吗?
  • 我已经给出了我的 start() 方法,我无法启动一个已经完成的线程
  • 您是否有可能两次启动同一个线程? (这是一个反问。)
  • 哇!解决了

标签: java exception awt illegalstateexception


【解决方案1】:

您似乎两次启动同一个Thread,这是不允许的。

如果您希望重复使用相同的线程,我建议您创建一个 ExecutorService

【讨论】:

    【解决方案2】:

    问题在于,如果 HelperThread 不为 null(它已经存在)并且您调用 HelperThread.start() 会尝试 start() 一个已经启动或完成它的处理的线程。如果要重新开始处理,则必须先停止线程。要优雅地执行此操作,请在实际线程中使用 while (running) { } 模式。然后,在 start() 方法中,将标志设置为 false,等待线程死亡,然后启动一个新线程:

    private void start() throws IllegalStateException {
        if (HelperThread != null) {
            running = false;
            HelperThread.join(); // wait for it to die...
        }
    
        // Start fresh...
        HelperThread = new Thread(this);
        HelperThread.start();
    }
    

    另外,作为旁注,请避免使用大写字母作为变量的首字母。它应该是“helperThread”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-27
      • 2013-01-13
      • 2017-08-09
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多