What are worker threads, and what is their role in the reactor pattern?

What are worker threads, and what is their role in the reactor pattern?

I assume you are talking about something like this documentation on thread-pools:

Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.

Worker threads are normal threads but they exist separate from the Runnable or Callable classes that they work on. If you extend Thread or you construct a Thread with a Runnable argument, the task is tied to the Thread object directly.

When you create a thread-pool using Executors.newFixedThreadPool(10); (or other similar methods), you create a pool of 10 threads that can run any number of different Runnable or Callable classes that are submitted to the pool. Underneath the covers they are still a Thread just more flexible because of the way they are wrapped.

In terms of the reactor pattern, different types of events are run by the handler threads which is similar. A thread is not tied to a single event class but will run any number of different events as they occur.


The Reactor pattern is used with worker threads to overcome a common scenario in applications: You need to do a lot of work eventually but you don't know which work and when and creating threads is an expensive operation.

The idea is that you create a lot of threads which don't do anything at first. Instead, they "wait for work". When work arrives (in the form of code), some kind of executor service (the reactor) identifies idle threads from the pool and assigns them work to do.

That way, you can pay the price to create all the threads once (and not every time some work has to be done). At the same time, your threads are generic; they will do whatever work is assigned to them instead of being specialized to do something specific.

For an implementation, look at thread pools.