BufferedImage Stream Access Secure

In Java, I have 2 threads that simultaneously access (without changing) the same BufferedImage. I just draw a buffer image into separate Graphics2D objects with this code.

Graphics2D g = getGraphics(); g.drawImage(myImage, 0, 0, null); 

Is there any reason why I need to synchronize access to images?

I know AWTEventThread is not thread safe, etc. I just create BufferedImages in the background thread.

Thanks a lot...

+4
source share
2 answers

(The name of your question does not actually correspond to the scenario described in the body, so I assume that you are asking about both cases ...)

Two threads that simply access (at this point), not changing BufferedImage , do not need to synchronize with each other.

However, it is necessary to establish a connection between the thread that created and initialized the BufferedImage object in the first place, and any threads that subsequently read it. Without this synchronization point, read streams can see stale values ​​for parts of the image data structure.

+4
source

Even in another read-only thread, changes made to the EDT must become visible, and this requires some form of synchronization to create before the relationship occurs . Several alternatives are shown here .

+2
source

All Articles