java - ThreadPoolExecutor application does not Finish -
this super simple application prints "hello" not finish. see absolutely no reason why should be.
javadoc, section finalization, says
a pool no longer referenced in program , has no remaining threads shutdown automatically.
tpe
not referenced, means thread not finish. don't understand why. explain?
solution in case call shutdown() @ end of main, actual application more complicated. new work generated inside of runnables, don't know when processed.
so, need figure out when call shutdown? or possible somehow specify that, when tpe
's queue empty, should shut down?
public class javaapplication5 { public static void main(string[] args) { threadpoolexecutor tpe = new threadpoolexecutor(5, 15, 10, timeunit.seconds, new linkedblockingqueue<runnable>()); tpe.execute(new runnable() { @override public void run() { system.out.println("hello"); } }); }
}
when main method finishes executor has no tasks left, still has threads running.
set flag allowcorethreadtimeout true before submitting tasks. when executor goes out of scope main method finishes, , tasks finish, threads terminated. see finalization in threadpoolexecutor api documentation:
a pool no longer referenced in program , has no remaining threads shutdown automatically. if ensure unreferenced pools reclaimed if users forget call shutdown(), must arrange unused threads die, setting appropriate keep-alive times, using lower bound of 0 core threads and/or setting allowcorethreadtimeout(boolean).
Comments
Post a Comment