- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
ThreadPool 利用线程回收来获得比使用多个 Thread 类更佳的性能。但是,这如何适用于在 ThreadPool 内部使用 while 循环的处理方法?
例如,如果我们将 ThreadPool 中的线程应用于已连接到 TCP 服务器的客户端,则该客户端将需要一个 while 循环来不断检查传入数据。可以退出循环以断开客户端,但前提是服务器关闭或客户端要求断开连接。
如果是这样的话,那么当大量客户端连接时,线程池有什么帮助呢?无论哪种方式,如果客户端保持连接,则使用相同数量的内存。如果它们保持连接,则线程无法回收。如果是这样,那么在客户端断开连接并打开一个线程进行回收之前,线程池将无济于事。
另一方面,有人建议我使用 Network.BeginReceive 和 NetworkStream.EndReceive 异步方法来避免线程一起使用以节省 RAM 使用和 CPU 使用。这是真的还是假的?
最佳答案
Either way the same amount of memory is used if the clients stay connected.
到目前为止,这是真的。由您的应用决定它需要为每个客户端保留多少状态。
If they stay connected, then the threads cannot be recycled. If so, then ThreadPool would not help much until a client disconnects and opens up a thread to recycle.
这是不正确的,因为它假定这些线程执行的所有有趣操作都是同步的。这是一种简单的操作模式,实际上现实世界的代码是异步的:一个线程调用请求一个 Action ,然后可以自由地做其他事情。当一个结果作为该操作的结果可用时,一些寻找其他事情要做的线程将运行对结果起作用的代码。
On the other hand it was suggested to me to use the Network.BeginReceive and NetworkStream.EndReceive asynchronous methods to avoid threads all together to save RAM usage and CPU usage. Is this true or not?
如上所述,像这样的异步方法将允许您只使用少量工作线程来为可能非常多的客户端提供服务——但它本身不会帮助或损害内存情况。
关于c# - ThreadPool 和带有 while(true) 循环的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23689074/
我在我的应用程序中使用 Windows ThreadPools,每次调用 CreateThreadPoolWork() 时都会遇到 136 字节的内存泄漏,如通过 UMDH 所见: + 1257728
摘自对我之前一个问题的回答( Task.Factory.StartNew starts with a great delay despite having available threads in t
这有什么区别?请引用选项1和选项2。因为我遇到了麻烦,因为它们好像是一样的。它们运行正确 Thread ThreadPoolExecutor executor = (ThreadPoolExecuto
Windows Phone 8 SDK 文档没有描述 Windows.System.Threading.ThreadPool 之间的区别。和 System.Threading.ThreadPool .
我无法通过JSP使用Elasticsearch Java API。在下面,我试图解释我所做的事情。 :| 我已经按照 flex 指令在系统上安装了elasticseach 2.3.3,并在命令提示符下
我试图在 Thread 的帮助下找出控制台应用程序中运行的线程数: new Thread(() => { while (true) {
我们对来自 Android 应用程序的所有网络流量使用 Retrofit/OkHttp3。到目前为止,一切似乎都很顺利。 但是,我们现在偶尔会遇到我们的应用程序/进程用完文件句柄的情况。 Androi
我们应该为长时间运行的线程使用线程池还是启动我们自己的线程?有什么设计模式吗? 最佳答案 不幸的是,这取决于。没有硬性规定说您应该始终使用线程池。 线程池提供两个主要功能: 线程的委托(delegat
我只是实现了一个线程池,如这里所述 Allen Bauer on thread pools 非常简单的实现,可以正常工作,但是我的应用程序不再关闭。似乎有两个工作线程(和另一个线程,我想是排队线程)卡
我有以下代码: static void Main(string[] args) { Console.Write("Press ENTER to start..."); Console.
我在下面编写了一个示例程序。 class Program { static int x = 2; static void Main(string[] args)
我试图了解Parralel.For和ThreadPool.QueueUserWorkItem之间的区别。 硬件和软件: 英特尔i5(四核) Windows 7 64位教授 DotNet 4.5 案例1
当用户单击按钮时,我使用ThreadPool.QueueUserWorkItem生成了一个启动长时间运行的线程的线程。我想在线程完成时使按钮可见,以便用户可以单击它。 ThreadPool中是否有已完
我想要一个类似的功能: public static V callAsyncAndWait(Func func) { ThreadPool.QueueUserWorkItem(obj =>
我正在尝试编写一个 Java 多线程程序,对作为文件给出的 2 个矩阵执行乘法,并使用有限的线程总数。 例如,如果我将线程数设置为 16,我希望我的线程池能够重用这 16 个线程,直到所有任务完成。
我有一个 C# 控制台应用程序,其中有一个线程池。在线程池中会有一个类执行一个连续的方法(直到它运行了一段时间或者知道什么时候停止)。该方法连接到 HttpWebResponse 流并继续读取它。 问
我正在使用 Reflector 仔细阅读 .Net ThreadPool 的一些源代码,当它显示以下内容时: private static bool QueueUserWorkItemHelper(W
每当用户扫描条形码时,我都会触发一个线程。 大多数时候它是一个相当短的运行线程。但有时可能需要很长时间(等待调用 GUI 线程)。 我读到过,为此使用 ThreadPool 可能是个好主意,而不是为每
我有一个场景,我试图通过在可能的情况下在用户实际需要之前预取结果的一些子元素来转变为响应速度更快的 UI。我不清楚如何最好地处理线程,所以我希望有人可以提供一些建议。 场景 有一个搜索表单(.NET
我需要对事件进行排队并并行执行它们。 我的 C# 代码在阻塞集合中对事件进行排队,并使用 ThreadPool 在工作线程上执行每个事件。但是,如果事件以 2000 事件/秒或更高的速率排队,则会错过
我是一名优秀的程序员,十分优秀!