Make thread run on non EDT (event dispatch thread) thread from EDT

一曲冷凌霜 提交于 2019-11-29 07:51:50

You can create and start a new Java Thread that executes your method from within the EDT thread :

@Override
    public void actionPerformed(ActionEvent arg0) {

        Thread t = new Thread("my non EDT thread") {
            public void run() {
                //my work
                new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
            }

        };
        t.start();
    }

You can use SwingWorker to undertake a task on a worker thread off the EDT.

E.g.

class BackgroundTask extends SwingWorker<String, Object> {
    @Override
    public String doInBackground() {
        return someTimeConsumingMethod();
    }

    @Override
    protected void done() {
        System.out.println("Done");
    }
}

Then wherever you call it:

(new BackgroundTask()).execute();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!