开发者

Using extended Swing component, threading doubt

I have built my own component extending JPanel. I've added a few methods like calculateWhatever()... Should I call those methods with invokeLater() as well? Substance L&F is not complaining about it but I'm having rare random painting problems and I'm wondering If they're happening because of those 开发者_开发百科calculations inside the extended swing component.


If you are doing a large number of calculations then it would be prudent to execute them in a thread other than the event dispatch thread. If you are using JDK 1.6 you could also use the SwingWorker class rather than invokeLater.


You only need to invoke these methods using invokeLater() (or using the SwingWorker) when you are modifying your component (or painting on it) from within these methods. If you merely do calculations you only need to synchronize when accessing the result in e.g. the paint() method.


You should generally use SwingUtilities.invokeLater or the SwingWorker to isolate the changes to GUI components from any potential locking scenarios (file I/O or remote server calls) or processor intensive operations. If your calculations do any disk reading or writing, or if you make calls to a server, I would suggest that you reorganize your code to do all of you painting calls in the AWT thread. Likewise if you realize your calcs are taking too long to complete and your GUI seems sluggish, breaking them out would be prudent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜