java - Future waiting for FixedThreadPool is returning before all Threads finish -
i'm trying wait threads finish before execute task using future, wrong because future wainting lastly thread of loop.
my executor method:
public static future<?> downloadimages(executor e, mainviewcontroller controller, string filepath, string dns, int port, int numimg, string offlineuuid, map<string, string> cookies, string type, string outputfolder) throws systemexception, ioexception, interruptedexception { string urlimages; string filepath2; future future = null; if (numimg == 1) { //some code } else { type = "multimages"; executorservice es = executors.newfixedthreadpool(numimg); (int = 0; < numimg; i++) { filepath2 = ""; filepath2 = filepath + file.separator + "targetapp" + file.separator + "tempimage" + + "download.zip"; urlimages = "http://" + dns + ":" + port + constants.target_service_downloadimages_path + offlineuuid + "/?pos=" + (i); future = es.submit(new downloaderandunziptask(controller, urlimages, filepath2, outputfolder, cookies, type)); } homecoming future; } homecoming null; }
my waiting method:
future future = fulldownloadselected(tableviewfull.getselectionmodel().getselectedindex()); if (future != null) { seek { future.get(); if (future.isdone()); system.out.println("processamento de imagens acabou"); } grab (executionexception ex) { logger.getlogger(mainviewcontroller.class.getname()).log(level.severe, null, ex); }
my msg shown when lastly thread created in first method finished, should have finished when threads in pool finished. think wrong submit executor within loop, how can prepare it?
you need capture every future returned , wait each 1 finish (using on each)
you can, alternatively, like:
executorservice es = executors.newfixedthreadpool(numimg); list<callable> tasks = ... (int = 0; < numimg; i++) { tasks.add(your tasks); } list<future<object>> futures = es.invokeall(tasks);
which homecoming 1 time tasks within complete.
java multithreading
No comments:
Post a Comment