使用Swing线程安全创建图像吗? [英] Is creating an image with Swing thread-safe?
问题描述
我想使用此Swing代码段从非基于SWing的应用程序的文本标签中创建图像(使用 Play框架编写的网络服务.
I want to use this Swing snippet to create an image from a text label from a non-Swing based app (a web service written using Play Framework, to be specific).
我从未使用过Swing,并且看到它不是线程安全的".这是否适用于我计划运行的最少代码?我应该同步访问此代码吗?
I've never used Swing, and saw that "it is not thread safe". Does this apply to the minimal code that I'm planning to run? Should I synchronize access to this code?
如果我了解有关正确地摆动线程安全性的答案, ,那么除非有一个方法明确指出它是安全的,否则它就不是...,而我使用的方法(特别是BufferedImage.getGraphics()
似乎没有这一段Javadoc).因此,除非另有说明,否则我将进行同步.
If I understand the answers about Swing thread-safety correctly, then unless a method explicitally says it's safe, then it isn't ... and the methods I use (specifically BufferedImage.getGraphics()
don't seem to have this piece of javadoc). So, unless shown otherwise, I'm going to synchronize.
推荐答案
不是线程安全的"意味着您不能一次从多个线程访问同一事物.
只要您不跨线程共享对象,就可以在后台线程上运行该代码没有任何问题.
"Not thread-safe" means that you must not access the same thing from multiple threads at once.
There is nothing wrong with running that code on a background thread, as long as you don't share the objects across threads.
请注意,大多数(非UI)对象对于只读操作都是线程安全的.
Note that most (non-UI) objects are thread-safe for read-only operation.
这篇关于使用Swing线程安全创建图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!