java - What of these two multithrading layouts is better? -
i have multithread layout there manager object , lot of workers objects.
i have doubt in layout better use:
1 - workers run in loop , ask "new job" manager after finished.
or
2 - manager give new jobs workers after finish each job.
are there recommendations this?
this question have wrestled many times. each time have chosen specific situation coding for. should same.
however, chose correctly must study 2 approaches carefully.
consider test case.
you have thousands of files process.
1. workers in control.
the manager
becomes queue of of files process. create fixed number of worker
threads request next file manager , repeat until list exhausted.
consequences
you end having synchronize
access queue.
you can tinker number of workers attain maximal throughput hardware architecture.
sometimes can dynamically adjust number of workers depending on current load can tricky. if successful can achieve exceptionally optimal solution.
2. manager in control.
the manager
creates new callable
every file , adds executor
controlled thread pool.
consequences
well ... same if think it. difference executor queueing.
there less synchronization required (except of course internally in executor
).
dynamically adjusting number of threads not trivial expect 1 subclass executor
achieve this.
in summary
the 2 architectures same. number of threads process sequence of items in parallel.
the differences more in dynamics , footprint.
when workers in control, known number of objects present @ time. extensive queue can build these presumably small objects. work done @ fixed , predicable pace. if work starts pile have make special effort deal it.
when manager in control there can explosion of workers, of sitting around waiting executor. essentially, executor becomes manager , thread pool holds workers.
i prefer workers being in control. suppose because given 2 similar architectures prefer 1 predictable footprint. plan experiment.
Comments
Post a Comment