What is the cause and resolution of java.lang.ExceptionInInitializerError error when trying to open a JFrame in another Thread?

前端 未结 2 1025
遥遥无期
遥遥无期 2021-01-24 08:11

I\'m trying to creating a test class to open a JFrame. In order to stop the window from closing the moment the main thread finishes I added the code to open up the window in ano

相关标签:
2条回答
  • 2021-01-24 08:41

    the problem occurs when you try to set a Thread to Daemon(true). When you exit your application the thread cannot be stopped and hence throws an java.lang.IllegalStateException: Shutdown in progress

    when you came over this problem you have to explicitly tell the Runtime to close the thread anyway, by adding a shotdownhook.

    public void createIntegerClass() throws Exception {
        Thread t = new Thread("Test Thread") {...};
    
        Runtime.getRuntime().addShutdownHook(t);//explicit!
        t.start();
        t.setDaemon(true);
    }
    
    0 讨论(0)
  • 2021-01-24 08:44

    The exception message tells you exactly what is wrong: you're trying to create a new thread while the JVM is shutting down.

    The reason the JVM shuts down when the main thread finishes is because you call setDaemon(true) on your event thread. Remove that line and the JVM will stay up as long as that thread is alive.

    0 讨论(0)
提交回复
热议问题