The first problem is the unnecessary wait that your producer has:
if(null!=queue.peek()){
This is all you need:
queue.add(image); notify();
The next question is unnecessary notify for your consumer. This gives you control over its processing at that moment, which, I believe, you intend to make your producer, but, of course, your code never comes to that. So:
}else{ bufferedImage = queue.poll(); notify(); } File imageFile = getFile(); if (!imageFile.getParentFile().exists()) { imageFile.getParentFile().mkdirs(); } try { ImageIO.write(bufferedImage, extension, imageFile); //Image saved catch (IOException e) { tracer.severe("IOException occurred. Image is not saved to file!"); } }
It should look bigger:
}else{ bufferedImage = queue.poll(); File imageFile = getFile(); if (!imageFile.getParentFile().exists()) { imageFile.getParentFile().mkdirs(); } try { ImageIO.write(bufferedImage, extension, imageFile); //Image saved catch (IOException e) { tracer.severe("IOException occurred. Image is not saved to file!"); } }
source share