hi have probleme thread. want 1 method n thread , don't know how it. : have list of new objects : create class(1) implements interface callable use because callable thread don't return void. next have list of string(url) next create list of object class (1) . next create executor , want thread on list of obcject n-thread
public class utltoimageconverter implements callable<bufferedimage> { private string url; private static bufferedimage image; public utltoimageconverter(string url) { this.url = url; getimgfrompath(url); } public string geturl() { return url; } public void seturl(string url) { this.url = url; } public bufferedimage getimage() { return image; } public void setimage(bufferedimage image) { utltoimageconverter.image = image; } public static bufferedimage getimgfrompath(string path){ if(testurlimage4.imgcache.get(path) != null){ return testurlimage4.imgcache.get(path); } else{ url url = null; try { url = new url(path); } catch (malformedurlexception e) { e.printstacktrace(); } try { image = imageio.read(url); } catch (ioexception e) { e.printstacktrace(); } system.out.println(testurlimage4.imgcache.size()); testurlimage4.imgcache.put(path, image); return image; } } @override public bufferedimage call() throws exception { system.out.println("url " +url); getimgfrompath(url); system.out.println("url ok " +url); return image; } }
next :
arraylist<utltoimageconverter> threadlist = new arraylist<>(); for(string url : urlpathlist){ threadlist.add(new utltoimageconverter(url)); } executorservice executor = executors.newfixedthreadpool(10); try { // executor.invokeall(threadlist, 1000l, timeunit.seconds); executor.invokeall(threadlist); // executor.invokeany(threadlist); } catch (interruptedexception e1) { e1.printstacktrace(); // } catch (executionexception e) { // e.printstacktrace(); }
the problem call getimgfrompath()
constructor of utltoimageconverter
. so, images downloaded in process of instantiation, sequentially, before being submitted executor. executor retrieves them cache, don't see difference. remove getimgfrompath()
call constructor , let executor actual downloading.
Comments
Post a Comment