Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class CustomImageWriter {
- private ExecutorService executorService;
- public CustomImageWriter() {
- this.executorService = Executors.newFixedThreadPool(1);
- }
- public void writeImage(final BufferedImage image, final File file) throws IOException {
- //Slow
- ImageIO.write(image, "PNG", file);
- //My suggestion
- Runnable r = new Runnable() {
- @Override
- public void run() {
- try {
- BufferedOutputStream imageOutputStream = new BufferedOutputStream(new FileOutputStream(file));
- ImageIO.write(image, "PNG", imageOutputStream);
- imageOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- executorService.submit(r);
- }
- public void waitForImages() {
- executorService.shutdown();
- try {
- executorService.awaitTermination(1, TimeUnit.DAYS);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement