Java Multithreading - Starting a lot of threads and keeping them idle, vs lazy loading new threads? -
i'm in situation have 4 groups of 7 threads each. cpu (core i7) supposed able handle 8 threads, i'm considering going through each group 1 @ time, running 7 threads, moving 2nd group, running 7 threads, 3rd , 4th groups in same way, , starting @ 1st group, until user sends stop command.
my question is, once each group of 7 threads has finished processing, should keep threads idle, or shut them down , restart new group of 7 threads @ next iteration? method faster? speed intensive app, need happen possible.
i using fixedthreadpool manage each group of 7 threads. either invokeall() , leave them alone (presumably idle), or shutdown() each threadpool after invokeall() , start new thread pool @ next iteration.
which method faster?
my question is, once each group of 7 threads has finished 1 cycle of processing, should keep threads idle, or shut them down , restart new group of 7 threads @ next cycle?
i use single executorservice thread-pool , reuse same threads all tasks. see tutorial on subject. thread-pool designed execute runnable or callable class task agnostic. example, might have parentresult , childresult classes. can submit callable<parentresult> thread-pool return future<parentresult> and can submit callable<childresult> same thread-pool return future<childresult>.
the reason why you'd want have "groups of threads" if each thread has state must maintain -- database connection or something. many people use thread-pools since of concurrency heavy lifting you.
if have keep state not shutdown pools , restart them later. dormant thread/pool taking no system resources aside memory. reason why ever if forking 100s of thread task @ point, should consider re-architecting application.
Comments
Post a Comment