multithreading - Java ExecutorService - why does this program keep running? -
i'm trying build background task executor terminates background tasks after time if there's no answer (background tasks call webservices , can time-out need make sure time out under time)
so have experiment if run program not terminate. wonder if because background thread still active maybe? how can shut down?
public class test { public static class task implements callable<object> { @override public object call() throws exception { while(true) {} } } public static void main(string[] args) { try { task t = new task(); executorservice executor = executors.newsinglethreadexecutor(); executor.invokeall(arrays.aslist(t), 5l, timeunit.seconds); executor.shutdown(); system.out.println("done"); } catch (interruptedexception e) { e.printstacktrace(); } }
}
the executorservice not kill running threads, , since threads created non-daemon, jvm doesn't exit.
what happens when timeout expires, futures returned invokeall() cancelled, means flag set on future object , cancellationexception
if try call future.get()
. neither invokeall(), nor shutdown() (or shutdownnow()) kill thread.
note cannot kill threads yourself. can setting application-specific flag or call thread.interrupt()
, not guarantee the thread terminates.
Comments
Post a Comment