简体   繁体   English

如何在并行执行上下文中找出我需要的N个线程?

[英]How to figure out N threads I need in a parallel execution context?

I'm working in a redelivery system. 我正在重新交付系统中工作。 This system attempt to execute an action, if the action fails, it try to execute again two times with an interval of five minutes, so I use the ExecutorService implementation to perform the first execution and ScheduledExecutorService to schedule the other ones, depending of its results (fail). 此系统尝试执行一个动作,如果该动作失败,它将尝试以五分钟的间隔再次执行两次,因此我将使用ExecutorService实现执行第一次执行,并使用ScheduledExecutorService来安排其他执行,具体取决于执行结果(失败)。

What should I consider to figure out the number of threads I need? 我应该考虑什么来确定所需的线程数? In this moment I use only a single thread model (created by newSingleThreadScheduledExecutor method) 目前,我仅使用一个线程模型(由newSingleThreadScheduledExecutor方法创建)

You should only need the one thread as only one action is running at a time. 您一次只需要运行一个操作,因此只需要一个线程。 You could use a CachedThreadPool and not worry about it. 您可以使用CachedThreadPool而不用担心。

Without knowing details about the load your system has, environment it is using and how long does it take to process one message it is hard to say which number of threads you need. 在不了解有关系统负载,系统正在使用的环境以及处理一条消息所需的时间的情况下,很难说出您需要多少线程。 However, you can think of the following base principles: 但是,您可以考虑以下基本原则:

  1. Having many threads is bad, because you'll spend significant amount of time on a context switch, the chance of starvation and wasting system resources is higher . 有很多线程是不好的,因为您将在上下文切换上花费大量时间,因此饥饿和浪费系统资源的机会更高。
  2. Each thread consumes some space in memory for its stack. 每个线程都会为其堆栈占用一些内存空间。 On x64 it is typically 1MB per thread. x64 ,每个线程通常为1MB

I would probably create 2 thread pools (one scheduled, one non-scheduled) for both sending and redelivery and test them under high load varying number of threads from 2 to 10 to see which number suits best. 我可能会为发送和重新交付创建2个线程池(一个预定的线程池,一个非预定的线程池),并在高负载下(从2到10)更改线程数来测试它们,以查看哪个数字最合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据教授需要弄清楚如何操纵变异子以找出一个类,我对此感到困惑 - need to figure out how to manipulate the mutator to figure out a class according to professor, I am confusd on how to do that 循环? 我需要弄清楚如何解决这个问题。 - Loops? I need to figure out how to fix this. 如何为有意义的相等对象创建线程锁/同步并防止各个线程的并行执行? - How can I create a thread lock/synchronization for meaningfully equal objects and prevent parallel execution of the respective threads? 我需要用主线程来控制这两个线程的执行吗? - Do I need to control the execution of these two threads with the main threads? 并行执行和终止多个线程 - Parallel execution and termination of multiple threads 需要拆分一个线程,使其在多个线程中运行。 还需要找出同步问题-JAVA - Need to split a thread so it runs in multiple threads. Also need to figure out a synchronized issue - JAVA 试图弄清楚线程和Java - Trying to figure out threads and Java 上下文切换与并行执行 - Context Switching vs Parallel Execution 如何确定需要从Java库导入的命名空间? - How do I figure out what namespace I need to import from a java library? 我需要在AsyncTask执行中传递变量,但无法弄清楚该怎么做 - I need to pass a variable in AsyncTask execute but can't figure out how to do it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM