I'm using some 3rd party AWT components in a desktop application. The component's layout is changed within the paint()
method, and 开发者_开发技巧this causes some very strange behaviour. This appears to be fixed by adding the synchronized
keyword to the paint() method, but is this a safe thing to do?
It looks like the paint()
method is called outside the event dispatch thread, which can indeed cause very strange behaviour, which is why it should never be done.
Instead of paint()
, application code should only ever call repaint()
The paint method should only be called within one thread, the Event Dispatch Thread, so there is no need to synchronize. I would imagine that the root of the problem lies in how the components are being used. Take a look at this link for some ideas around concurrency in the UI.
精彩评论