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

后端 未结 3 1186
野性不改
野性不改 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:05

    I can't think of much technical difference other than mere terminology.

    Worker Threads called so because they are waiting for some job to come and does the job when assigned by someone else. For example, a web server process receives request and assign it to a thread from its pool for processing. That thread obeys the process and finishes the work and returns back to pool. Till then the main thread will be doing something else.

    For your purpose: Monitoring DB continuously is required for identifying updated/new values. It can just be a thread, running always in background, wakes up periodically and updates the values in UI from DB.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 09:29

    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.

    0 讨论(0)
提交回复
热议问题