I'm trying to write in parallel an image which gets modified each iteration of a loop. The code looks like:
ExecutorService pool = Executors.newFixedThreadPool(8);
for (int i = array.length - 1; i > 0; i--)
{
pool.execute(() -> {
ImageIO.write(bufferedImage, "JPG", fileName i);
});
//Operations on the image...
}
The problem is that the image gets written just at its initial state (i=array.length) and at its final (i=1), but it actually writes the numbers of files it should (array.length times). So my question is, do I have to synchronize something on the image I'm writing? Aren't the objects state snapshotted and saved in memory once the Task gets into the queue? Or do tasks get the state of the object at the time of their actual execution?
CodePudding user response:
Task get state on moment of execution.
There is no straightforward way to get snapshot of object in java.