开发者

Is accessing BufferedImage thread safe

开发者 https://www.devze.com 2023-03-12 07:58 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消