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

前端 未结 2 1027
遥遥无期
遥遥无期 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);
    }
    

提交回复
热议问题