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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With