Java GUI freezes even with SwingWorker

I am trying to use SwingWorker to perform a lengthy task and update JLabel with the result:

button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new SwingWorker<String, Void>() { @Override protected String doInBackground() throws Exception { return doCalculation(); } protected void done() { try { label.setText(get()); } catch (InterruptedException e) { System.out.println("thread was interrupted"); } catch (ExecutionException e) { System.out.println("there was an ExecutionException"); } } }.execute(); } }); 

I can press the button many times as I like, and Gui will remain responsive until two threads are completed, after which Gui freezes while the threads are running. If I only run one thread at a time, this problem still occurs.

It would be great if someone could indicate if I am using SwingWorker incorrectly or if there is another issue that I do not know about. Thank you for your time. Ian

Edit

DoCalculation () is just a lot of time:

 private String doCalculation() { for (int i = 0; i < 10000000; i++) { Math.pow(3.14, i); } return threads++ + ""; } 
+6
source share
1 answer

Sorry, but even with the doCalculate () method, I'm still not able to reproduce your problem. For example, here is my sscce :

 import java.awt.event.*; import java.util.concurrent.ExecutionException; import javax.swing.*; public class FooGui { private static int threads = 0; private static void createAndShowUI() { final JLabel label = new JLabel(" "); JButton button = new JButton("Button"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new SwingWorker<String, Void>() { @Override protected String doInBackground() throws Exception { return doCalculation(); } @Override protected void done() { try { label.setText(get()); } catch (InterruptedException e) { System.out.println("thread was interrupted"); } catch (ExecutionException e) { System.out.println("there was an ExecutionException"); } } }.execute(); } }); JPanel panel = new JPanel(); panel.add(button); panel.add(label); JFrame frame = new JFrame("FooGui"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static String doCalculation() { for (int i = 0; i < 5000000; i++) { Math.pow(3.14, i); } return threads++ + ""; } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { createAndShowUI(); } }); } } 

You might want to create and publish "Short, Self Contained, Correct (Compilable), Example" or SSCCE (please check the link). I will bet that in the process of creating this, you are likely to find the problem and its solution on your own. If so, please come back here and let us know.

I know that this is actually not an answer, but there is no way to post such code in a comment.

+6
source

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


All Articles