Create a thread for a thread without EDT (event dispatch stream) from EDT

I have a method that works on EDT, and inside this I want to make it execute something on a new (not EDT) thread. My current code is as follows:

@Override public void actionPerformed(ActionEvent arg0) { //gathering parameters from GUI //below code I want to run in new Thread and then kill this thread/(close the JFrame) new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState); } 
+4
source share
2 answers

You can create and run a new Java thread that executes your method from 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(); } 
+3
source

You can use SwingWorker to complete a task on a SwingWorker with EDT.

eg.

 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(); 
+7
source

Source: https://habr.com/ru/post/1303983/


All Articles