1

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
thelinh bui
  • 226
  • 6
  • 14

2 Answers2

4

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.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • I dont think so ... because the variable need to attach to the thread and increase gradually and the JSlider moving according to the variable value. If i used just the while loop to do this. The interface will be freezed – thelinh bui Sep 07 '12 at 14:00
  • 1
    Then, yes, you can use SwingWorker. But there is also javax.swing.Timer, which is made for what you seem to be describing in the comment. – Steve McLeod Sep 07 '12 at 14:05
  • Do i have to call repaint() on the JSlider. Thanks – thelinh bui Sep 07 '12 at 14:09
  • 3
    Please just, one question per page - but no, you don't need to call repaint. – Steve McLeod Sep 07 '12 at 14:15
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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045