java - Does ThreadPoolExecutor spawns a new thread if a current thread sleeps -
this question followup on this one.
essentially doing declaring threadpoolexecutor
1 thread. overriding beforeexecute()
method put sleep each of tasks executed delay among themselves. give away cpu other threads since thread kind of thrashing.
so expected behavior is:
for each new task in threadpoolexecutor
, calls before execute function before executing task , hence sleeps 20s before executes task.
however see:
for each new task submitted:
- it executes task
- calls beforeexecute method
- sleeps 20s
- re-execute task!
the order of 1. & 2. not same time.
here questions:
- it appearing new thread comes in after/during sleeping , goes ahead , executes task right away while actual thread sleeping.
threadpoolexecutor
spawn new thread existing thread sleeps [thinking thread terminated]?? tried put keepalivetime > sleeptime ..so in case above assertion true .. atleast waits more sleep time spawn new thread ...[hoping in mean time sleeping thread awake ,threadpoolexecutor
dump idea of spawning new thread - even if spawn new thread , execute task right away, why task re-executed after sleeping thread wakes !! shouldn't task taken out of task queue before ??
- am missing here ? other way debug scenario ?
=> alternative method thinking desired task [and not solve peoblem] wrap runnable 1 more runnable , sleep outer runnable before calling inner one.
i think you're looking scheduledexecutorservice
from understand of question, scheduleatfixedrate(...) should deal:
scheduleatfixedrate(runnable command, long initialdelay, long period, timeunit unit)
creates , executes periodic action becomes enabled first after given initial delay, , subsequently given period; executions commence after initialdelay initialdelay+period, initialdelay + 2 * period, , on.
Comments
Post a Comment