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:

  1. it executes task
  2. calls beforeexecute method
  3. sleeps 20s
  4. re-execute task!

the order of 1. & 2. not same time.

here questions:

  1. 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
  2. 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 ??
  3. 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

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -