Skip to content

Instantly share code, notes, and snippets.

@vikeshkumar
Last active December 31, 2015 21:09
Show Gist options
  • Save vikeshkumar/c85a1ffd840551c9a84b to your computer and use it in GitHub Desktop.
Save vikeshkumar/c85a1ffd840551c9a84b to your computer and use it in GitHub Desktop.
Thread Executor, Thread Pool simple example.
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.coobird.thumbnailator.Thumbnails;
/**
*
* @author Vikesh
*/
public class ThumbNailGen {
private static final String INPUT_DIR = "G:\\Images\\2013-12-19";
private static final String OUTPUT_DIR = "G:\\Images\\2013-12-19\\thumb";
private static final double DEFAULT_SCALE = 0.30;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
File inputDir = null;
File outputDir = null;
double scale = DEFAULT_SCALE;
if (args.length == 2) {
inputDir = new File(args[0]);
outputDir = new File(args[1]);
} else {
inputDir = new File(INPUT_DIR);
outputDir = new File(OUTPUT_DIR);
}
if (args.length == 3) {
scale = Double.parseDouble(args[2]);
}
if (!outputDir.exists()) {
outputDir.mkdir();
}
if (inputDir.isDirectory()) {
String[] list = inputDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
String fName = name.toLowerCase();
return fName.endsWith("jpg") || fName.endsWith("jpeg");
}
});
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (String file : list) {
final File img = new File(inputDir, file);
final File parent = outputDir;
final double tempScale = scale;
final String outName = file;
executorService.submit(new Runnable() {
@Override
public void run() {
try {
Thumbnails.of(img).scale(tempScale).toFile(new File(parent, outName));
} catch (IOException ex) {
Logger.getLogger(ThumbNailGen.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
executorService.shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment