C# 什么是工作线程及其与我创建的线程的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a worker thread and its difference from a thread which I create?
提问by Tj.
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..
这与 Worker 线程有什么不同吗?如果是..哪个更好,我什么时候应该使用工作线程?我的应用程序需要有很多线程进行监控,刷新..
回答by Preet Sangha
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.
通常,术语工作线程用于描述正在当前线程上执行工作的线程中的另一个线程 - 在很多情况下是前台或 UI 线程。然而,这并不是一成不变的。
Windows programs generally use a single primarythread 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.
Windows 程序通常使用单个主线程来管理 UI,并且这通常是同步的(即事情一个接一个地运行)。如果有一个长时间运行的任务要执行,那么为了避免在这些类型的程序中产生 UI 块,您可以使用工作线程(可以是前台线程或后台线程)来完成工作(与主线程异步) ,然后将结果呈现回主线程消费。
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.
在 Windows 程序中,这是通过消息完成的。如果您使用特定的库,例如 .net 框架,那么可以使用特殊的实用程序类(例如 ThreadPool 和 BackgroundWorker)来简化后台或工作线程处理。但与往常一样,您可以使用平台原语来实现相同的目的。
回答by vpram86
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.
Worker Threads 之所以这样称呼是因为它们正在等待某个工作的到来并在其他人分配时执行该工作。例如,Web 服务器进程接收请求并将其分配给其池中的线程进行处理。该线程服从进程并完成工作并返回到池中。在那之前,主线程将做其他事情。
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.
为了您的目的:需要持续监控数据库来识别更新/新值。它可以只是一个线程,始终在后台运行,定期唤醒并从 DB 更新 UI 中的值。
回答by amarnathpatel
I am trying to explain the concept in 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 order of his boss(we can call it “client” ) and update work result to boss.
“工作线程”只是一个线程,它按照老板(我们可以称之为“客户端”)的命令执行一些后台工作并将工作结果更新给老板。
Technical Definition:-
技术定义:-
A worker thread is usually defined as a thread that gets activated on clients requests.
工作线程通常定义为在客户端请求时被激活的线程。
Example 1:
示例 1:
1- We have pizza store, where there are 10 guys who are expert in preparing a delicious pizzas. These are called as "worker threads".
1- 我们有比萨店,那里有 10 个人擅长准备美味的比萨。这些被称为“工作线程”。
2- We have a guy who receives the orders from customers. That guy is called as “client”. Whenever a new order comes, one of "worker thread" starts preparing the pizza and update to client once the pizza is prepared.
2- 我们有一个人从客户那里接收订单。那个人被称为“客户”。每当有新订单出现时,“工作线程”之一就会开始准备比萨饼,并在准备好比萨饼后更新到客户端。
3- When there are less than 10 orders, some of the workers just sit ideal.
3- 当订单少于 10 个时,一些工人只是坐在理想的位置。
4- When there are more than 10 orders, the orders are just put into waiting queue.
4-当订单超过10个时,订单只进入等待队列。
Example 2:
示例 2:
1- There is an app server that listens to port 8080.
1- 有一个应用服务器监听 8080 端口。
2- A request comes in on port 8080.
2- 请求来自端口 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.
3- 侦听器线程(称为“客户端”)接收该请求并将其分派到完成请求的“工作线程”。实际上在应用服务器上维护了一个“工作线程”池(“工作线程”程序的许多对象)。
4- If two requests come in at the same time, two worker threads are assigned and the task is executed simultaneously.
4- 如果两个请求同时传入,则分配两个工作线程并同时执行任务。