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

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -