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

后端 未结 3 1187
野性不改
野性不改 2021-01-31 08:35

I create a thread by

Thread newThread= new Thread(DoSomeWork);

.
.
.
private void DoSomeWork()
{
}

Is this any different from a Worker thread?

3条回答
  •  失恋的感觉
    2021-01-31 09:11

    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 boss.

    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.

提交回复
热议问题