In Java, I have 2 threads that are both accessing (not modifying) the same BufferedImage. I'm simply drawing the buffered image into a separate Graphics2D objects with code like this.
Graphics2D g = getGraphics();
g.drawImage(myImage, 0, 0, null);
Is there any reason I need to synchronize a开发者_运维百科ccess to the images?
I know about the AWTEventThread not being thread safe etc. I'm just building some BufferedImages in a background thread.
Thanks much...
(The title of your question doesn't actually match the scenario described in the body, so I'm assuming that you are asking about both cases ...)
The two threads that are just accessing an (at that point) non-changing BufferedImage
would not need to synchronize between themselves.
However, there does need to be a happens-before relationship between the thread that created and initialized the BufferedImage
object in the first place and any threads that subsequently read it. Without that synchronization point, the reading threads might see stale values for parts of the image data structure.
Even in another, read-only thread, changes made on the EDT must become visible, and that requires some form of synchronization to create a happens-before relation. Several alternatives are shown here.
精彩评论