Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 782
Jon Avatar asked Nov 05 '12 15:11

Jon


People also ask

How do you think reactor pattern works in case of worker threads?

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.

How worker threads work?

So, how do worker threads actually work under the hood? The responsibility of a worker thread is to run a piece of code specified by the parent or main thread. Each worker runs in isolation from other workers. However, a worker and its parent can pass messages back and forth through a message channel.

What are workers in java?

A Worker is an object which performs some work in one or more background threads, and whose state is observable and available to JavaFX applications and is usable from the main JavaFX Application thread.

What is reactor pattern in Java?

The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. The application can register specific handlers for processing which are called by reactor on specific events.


1 Answers

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.

like image 82
Aaron Digulla Avatar answered Sep 21 '22 21:09

Aaron Digulla