Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a worker thread and its difference from a thread which I create?

I create a thread by

Thread newThread= new Thread(DoSomeWork);  . . . private void DoSomeWork() { } 

Is this any different from a Worker thread? If its is..which is better and when should I use a worker thread? My application needs to have lots of thread doing monitoring, refreshing..

like image 841
Tj. Avatar asked Oct 06 '09 08:10

Tj.


People also ask

What is the worker thread?

A “worker thread” is just a thread which runs to perform some background work on the order of his boss(we can call it “client” ) and update work result to the client.

What is difference between worker thread and main thread?

People use the word "worker" when they mean a thread that does not own or interact with UI. Threads that do handle UI are called "UI" threads. Usually, your main (primary) thread will be the thread that owns and manages UI. And then you start one or more worker threads that do specific tasks.

What is a worker thread in Java?

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.

How do I create a worker thread?

We use the Worker class to create a new worker thread. It accepts the following arguments when creating a new Worker instance. Here, the filename argument refers to the path to the file which contains the code that should be executed by the worker thread. Therefore, we need to pass the file path of the worker.


2 Answers

I am trying to explain the concept in a simple way, hope it will help to better understand the worker thread concept.

General Definition:-

A “worker thread” is just a thread which runs to perform some background work on the order of his boss(we can call it “client” ) and update work result to the client.

Technical Definition:-

A worker thread is usually defined as a thread that gets activated on clients' requests.

Example 1:

1- We have a pizza store, where there are 10 guys who are experts in preparing a delicious pizza. These are called as "worker threads".

2- We have a guy who receives orders from customers. That guy is called as “client”. Whenever a new order comes, one of "worker thread" starts preparing the pizza and updates to the client once the pizza is prepared.

3- When there are less than 10 orders, some of the workers just sit idle.

4- When there are more than 10 orders, the orders are just put into waiting queue.

Example 2:

1- There is an app server that listens to port 8080.

2- A request comes in on port 8080.

3- A listener thread (it’s called as “client”) takes that request and dispatches it to a “worker thread” that completes the request. There is actually a pool of “worker threads” maintained ( many objects of the “worker thread” program) on app server.

4- If two requests come in at the same time, two worker threads are assigned and the task is executed simultaneously.

like image 64
amarnathpatel Avatar answered Sep 27 '22 17:09

amarnathpatel


Generally the term worker thread is used to describe another thread from the one that is doing the work on the current thread - which in lots of cases is a foreground or UI thread. This is not cast in stone however.

Windows programs generally use a single primary thread to manage the UI and this is generally synchronous (i.e. things runs one after the other). If there is a long running task to perform then to avoid making the UI block in these kinds of programs you use a worker thread (which can be either a foreground thread or a background thread) to do the work (asynchronously to the primary thread), and then present the results back to the primary thread to consume.

In windows programs this is done via messages. If you use particular libraries such as say the .net framework, then special utility classes such as ThreadPool and BackgroundWorker are available to make background or worker thread handling easier. But as always you can using the platform primitives to achieve the same end.

like image 34
Preet Sangha Avatar answered Sep 27 '22 18:09

Preet Sangha