multithreading - java ThreadPoolExecutor time-out not honored -
in application have used thread pool, have specified timeout thread pool seems timeout not called, below code :
import java.sql.timestamp; import java.util.arraylist; import java.util.date; import java.util.concurrent.arrayblockingqueue; import java.util.concurrent.executionexception; import java.util.concurrent.future; import java.util.concurrent.threadpoolexecutor; import java.util.concurrent.timeunit; import java.util.concurrent.timeoutexception; public class threadpooldemo extends thread{ public void run(){ system.out.println("starting---" + new timestamp((new date()).gettime()) + "--" + thread.currentthread().getname()); try { thread.sleep(30000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("finishing---" + new timestamp((new date()).gettime()) + "--" +thread.currentthread().getname()); } public static void main (string[] args){ arrayblockingqueue<runnable> threadqueue = new arrayblockingqueue<runnable>(5); threadpoolexecutor thumbnailgeneratorthreadpool = new threadpoolexecutor(1, 3, 5, timeunit.seconds, threadqueue); thumbnailgeneratorthreadpool.allowcorethreadtimeout(true); arraylist ftasks = new arraylist(); (int = 0; < 15; i++) { system.out.println("submitting thread : " + (i+1) + "- current queue size : " + threadqueue.size()); threadpooldemo tpd = new threadpooldemo(); future future = thumbnailgeneratorthreadpool.submit(tpd); } } }
the out put of code :
submitting thread : 1- current queue size : 0 submitting thread : 2- current queue size : 0 submitting thread : 3- current queue size : 1 submitting thread : 4- current queue size : 2 submitting thread : 5- current queue size : 3 submitting thread : 6- current queue size : 4 submitting thread : 7- current queue size : 5 submitting thread : 8- current queue size : 5 submitting thread : 9- current queue size : 5 exception in thread "main" java.util.concurrent.rejectedexecutionexception @ java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution(threadpoolexecutor.java:1774) @ java.util.concurrent.threadpoolexecutor.reject(threadpoolexecutor.java:768) @ java.util.concurrent.threadpoolexecutor.execute(threadpoolexecutor.java:656) @ java.util.concurrent.abstractexecutorservice.submit(abstractexecutorservice.java:78) @ threadpooldemo.main(threadpooldemo.java:33) starting---2016-10-26 14:20:16.254--pool-1-thread-2 starting---2016-10-26 14:20:16.254--pool-1-thread-3 starting---2016-10-26 14:20:16.254--pool-1-thread-1 finishing---2016-10-26 14:20:46.261--pool-1-thread-1 finishing---2016-10-26 14:20:46.261--pool-1-thread-2 finishing---2016-10-26 14:20:46.261--pool-1-thread-3 starting---2016-10-26 14:20:46.261--pool-1-thread-2 starting---2016-10-26 14:20:46.261--pool-1-thread-3 starting---2016-10-26 14:20:46.261--pool-1-thread-1 finishing---2016-10-26 14:21:16.265--pool-1-thread-1 starting---2016-10-26 14:21:16.265--pool-1-thread-1 finishing---2016-10-26 14:21:16.265--pool-1-thread-3 starting---2016-10-26 14:21:16.265--pool-1-thread-3 finishing---2016-10-26 14:21:16.265--pool-1-thread-2 finishing---2016-10-26 14:21:46.277--pool-1-thread-1 finishing---2016-10-26 14:21:46.277--pool-1-thread-3
now in threadpoolexecutor
keepalivetime
set 5 seconds.
however if see output thread takes 30 seconds complete. i'm not sure why interruptedexception
not being called threadpoolexecutor
on thread.
i mechanism stop threads if thread still active beyond timeout specified.
as indicated in comments, did not specify time out. specified keep-alive time of threadpoolexecutor. keep-alive not terminate or interrupt running threads, releases idle threads of executor (see getkeepalivetime
).
if want set time out tasks, have use invokeall
or invokeany
methods instead of submit
. see also
Comments
Post a Comment