multithreading - Java: single-threaded sub-Executor within another multi-threaded Executor -
i'd run tasks in serialized way. typical solution create an
executor executor = executors.newsinglethreadedexecutor(); and run tasks on one.
however, have thread pool that's multi-threaded.
is there simple way of deriving sub-executor behaves single-threaded 1 (as in: runs 1 task @ time) using (possibly non-single-threaded) executor "backend" instead of creating brand new os thread?
there several use cases why want this:
- the app might have thread pool e.g. background tasks, set priority etc, we'd reuse.
- similarly, might pass in executor not plain thread pool (e.g. deferring execution later, measuring execution time etc.)
- a subset of passing in moreexecutors.directexecutor() testing (so e.g. futures resolve immediately).
edit: added above examples
such zero-threaded executor called serialexecutor , described in java documentation java.util.concurrent.executor. has little drawback, however: each submitted runnable creates wrapper object. my own implementation not create additional objects.
Comments
Post a Comment