简体   繁体   中英

Is it safe to synchronize java.awt.Container.paint(Graphics g)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM