同步 java.awt.Container.paint(Graphics g) 是否安全?
我在桌面应用程序中使用一些第三方 AWT 组件。组件的布局在 paint()
方法中发生了变化,这会导致一些非常奇怪的行为。这似乎可以通过将 synchronized
关键字添加到 Paint() 方法来解决,但是这样做安全吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
paint()
方法是在 事件调度线程,这确实会导致非常奇怪的行为,这就是为什么永远不应该这样做。应用程序代码应该只调用
重新绘制()
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 callrepaint()
Paint 方法只能在一个线程(事件调度线程)内调用,因此不需要同步。我想问题的根源在于如何使用组件。查看此链接了解一些想法UI 中的并发性。
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.