java - Why isn't this multithreaded code faster? -
this java code. before, calls batchgenerateresult
sequentially lengthy process, want try multithreading , have each 1 of them run @ same time. when test it, new time same old time. expected new time faster. know whats wrong?
public class plutomake { public static string classdir; public static void main(string[] args) throws jsonexception, ioexception, interruptedexception { // determine path class file, use current directory string classdirfile = plutomake.class.getresource("plutomake.class") .getpath(); classdir = classdirfile.substring(0, classdirfile.lastindexof("/") + 1); // input arguments final string logopath; final string filename; if (args.length < 2) { logopath = classdir + "tests/android.png"; filename = "result.png"; } else { logopath = args[0]; filename = args[1]; } // make sure logo image exists file logofile = new file(logopath); if (!logofile.exists() || logofile.isdirectory()) { system.exit(1); } // master.js file string text = readfile(classdir + "master.js"); jsonarray files = new jsonarray(text); executorservice es = executors.newcachedthreadpool(); // loop through active templates int len = files.length(); (int = 0; < len; += 1) { final jsonobject template = files.getjsonobject(i); if (template.getboolean("active")) { es.execute(new runnable() { @override public void run() { try { batchgenerateresult(logopath, template.getstring("template"), template.getstring("mapping"), template.getstring("metadata"), template.getstring("result") + filename, template.getstring("filter"), template.getstring("mask"), template.getint("x"), template.getint("y"), template.getint("w"), template.getint("h")); } catch (ioexception | jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } } es.shutdown(); boolean finshed = es.awaittermination(2, timeunit.minutes); } private static void batchgenerateresult(string logopath, string templatepath, string mappingpath, string metadatapath, string resultpath, string filter, string maskpath, int x, int y, int w, int h) throws ioexception, jsonexception { colorfilter filterobj = null; if (filter.equals("none")) { filterobj = new nofilter(); } else if (filter.equals("darken")) { filterobj = new darken(); } else if (filter.equals("vividlight")) { filterobj = new vividlight(); } else { system.exit(1); } string text = readfile(classdir + metadatapath); jsonobject metadata = new jsonobject(text); map<point, point> mapping = myjson.readmapping(classdir + mappingpath); bufferedimage warpedimage = exporter.generatewarpedlogo(logopath, maskpath, mapping, metadata.getint("width"), metadata.getint("height")); // imageio.write(warpedimage, "png", new fileoutputstream(classdir + // "warpedlogo.png")); exporter.stamplogo(templatepath, resultpath, x, y, w, h, warpedimage, filterobj); warpedimage.flush(); } private static string readfile(string path) throws ioexception { file file = new file(path); fileinputstream fis = new fileinputstream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); string text = new string(data, "utf-8"); return text; } }
it looks like, practical purposes following code should 1 can improve performance using multithreading.
bufferedimage warpedimage = exporter.generatewarpedlogo(logopath, maskpath, mapping, metadata.getint("width"), metadata.getint("height")); // imageio.write(warpedimage, "png", new fileoutputstream(classdir + // "warpedlogo.png")); exporter.stamplogo(templatepath, resultpath, x, y, w, h, warpedimage, filterobj);
the rest of major io - doubt how performance improvement can achieve there.
do profile , check how long each 1 of methods executing. depending on should able understand.
Comments
Post a Comment