Java Swingworker Thread

2019-07-29 07:24发布

问题:

I have a question to ask about Java Concurrency. I'm practicing Java Threading. What i do is create a simple interface which has a button and a JSlider. So when i click the button a variable will increase and the JSlider moving accordingly. Now I wonder if Swingworker is a right choice.

回答1:

You only need to use SwingWorker for long-running tasks. What you describe will be performed instantly. Therefore you can do this on the Event Dispatch Thread, where all ActionListeners are performed.



回答2:

You may want to profile your code first.

  • Examples where a state change does not block the EDT: here and here.

  • An example where an action might block the EDT: here.

  • An example where an action will block the EDT: here.