当使用多线程? [英] When to use Multithread?

查看:129
本文介绍了当使用多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你在一个应用程序中使用线程?例如,在简单的CRUD操作,使用SMTP的,调用Web服务,可能需要一些时间,如果服务器正面临着带宽的问题,等等。

When do you use threads in a application? For example, in simple CRUD operations, use of smtp, calling webservices that may take a few time if the server is facing bandwith issues, etc.

说实话,我不知道如何确定我是否需要使用一个线程(我知道,当我们外,用操作将需要几分钟的时间来完成它必须是)。

To be honest, i don't know how to determine if i need to use a thread (i know that it must be when we're excepting that a operation will take a few time to be done).

这可能是一个小白的问题,但如果你和我分享的主题体验这将是巨大的。

This may be a "noob" question but it'll be great if you share with me your experience in threads.

感谢

推荐答案

我加了C#和.NET标记你的问题,因为你提到你的标题C#。如果这是不准确的,随意删除标记。

I added C# and .NET tags to your question because you mention C# in your title. If that is not accurate, feel free to remove the tags.

有不同风格的多线程。例如,存在与回调函数异步操作。 .NET 4.0引入了并行LINQ库。多线程,你会使用,还是使用任何在所有的款式,取决于你正在努力完成的任务。

There are different styles of multithreading. For example, there are asynchronous operations with callback functions. .NET 4 introduces the parallel Linq library. The style of multithreading you would use, or whether to use any at all, depends on what you are trying to accomplish.

并行执行,比如什么并行LINQ一般会被试图做的,利用执行不需要等待来自对方的数据说明多个处理器内核。有许多来源的LINQ外这样的算法,如这个。但是,它有可能并行执行可能无法你或者说,它不适合你的应用程序。

Parallel execution, such as what parallel Linq would generally be trying to do, takes advantage of multiple processor cores executing instructions that do not need to wait for data from each other. There are many sources for such algorithms outside Linq, such as this. However, it is possible that parallel execution may be unable to you or that it does not suit your application.

更传统的多线程利用线程的.NET库中(在这种情况下)由 System.Thread 的规定。请记住,在开始在线程处理一些开销,所以只能使用线程时做的优势,使超过此开销。一般来说,你只需要使用这种类型的单处理器多线程当线程下运行任务将有很长的差距,其中处理器可以做别的事情。例如,输入/从硬盘(和,因此,从使用一个数据库系统)O的幅度比存储器存取速度慢的很多订单。网络访问也可以是缓慢的,作为另一个例子。多线程可能允许运行另一程序在等待这些慢(相比于处理器)的操作完成。

More traditional multithreading takes advantage of threading within the .NET library (in this case) as provided by System.Thread. Remember that there is some overhead in starting processes on threads, so only use threads when the advantages of doing so outweigh this overhead. Generally speaking, you would only want to use this type of single-processor multithreading when the task running under the thread will have long gaps in which the processor could be doing something else. For example, I/O from hard disk (and, consequently, from a database system that uses one) is many orders of magnitude slower than memory access. Network access can also be slow, as another example. Multithreading could allow another process to be running while waiting for these slow (compared to the processor) operations to complete.

当我使用了传统的多线程另一个例子是缓存第一次一个特定的ASP.NET页面在会话中访问某些值。我揭开序幕一个线程,使用户不必等待缓存与页面交互之前完成。我还调节时的行为缓存在用户请求另一页之前,使得,如果该高速缓存未完成,这不是一个问题不会完成。它只是让一些还要求更快的为previously太慢了。

Another example when I have used traditional multithreading is to cache some values the first time a particular ASP.NET page is accessed within a session. I kick off a thread so that the user does not have to wait for the caching to complete before interacting with the page. I also regulate the behavior when the caching does not complete before the user requests another page so that, if the caching does not complete, it is not a problem. It simply makes some further requests faster that were previously too slow.

此外还应考虑到多线程有你的应用程序的可维护性成本。线程应用程序可能难以调试,例如。

Consider also the cost that multithreading has to the maintainability of your application. Threaded applications can be harder to debug, for example.

我希望至少在某种程度上这回答你的问题。

I hope this answers your question at least somewhat.

这篇关于当使用多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆