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

Jon picture Jon · Nov 5, 2012 · Viewed 24k times · Source

I'm trying to understand the Reactor pattern (concurrent), but in many examples they are talking about 'worker threads'. What are worker threads? In what way do they differ from 'normal' threads? And what is their role in the reactor pattern?

Answer

Aaron Digulla picture Aaron Digulla · Nov 5, 2012

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.