27

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?

Draken
  • 3,049
  • 13
  • 32
  • 49
Jon
  • 427
  • 1
  • 5
  • 11
  • 3
    Please provide links to the references that you are talking about. – Gray Nov 05 '12 at 15:36
  • http://en.wikipedia.org/wiki/Reactor_pattern – Jon Nov 05 '12 at 16:15
  • I see nothing about "worker threads" on that site @Jon. – Gray Nov 05 '12 at 16:16
  • Right, but that was just for the reactor pattern, here is another http://today.java.net/pub/a/today/2007/02/13/architecture-of-highly-scalable-nio-server.html – Jon Nov 05 '12 at 16:20
  • Ok, right. In that link it shows that either you can define a thread attached to a particular thread or you can use a thread-pool where jobs can be submitted to the pool. The worker threads in the pool can run different types of jobs. But the article uses the term "worker threads" liberally there to just be threads. – Gray Nov 05 '12 at 16:24
  • 2
    Good answers below, but I think they leave out one point: There is _no difference_ between how a "worker thread" is implemented and how any other thread is implemented. The only difference is the code that they execute. We call them "worker threads" because they wait for work. That's all. – Solomon Slow Oct 06 '14 at 13:49

2 Answers2

38

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.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
20

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.

Gray
  • 108,756
  • 21
  • 270
  • 333
  • Thanks for the explanation, this really cleared it up. Is there a difference between the Runnable and Callable interfaces? – Jon Nov 05 '12 at 16:18
  • 5
    Take a look at the Javadocs @Jon. `Runnable` has a method `void run()` while `Callable` has a method `T call()`. `Callable` methods allow you to return a value from the job you submit to the worker pool. – Gray Nov 05 '12 at 16:19