简体   繁体   English

如何确定在java中创建的合适线程数?

[英]How to decide the suitable number of threads to create in java?

I have a java application that creates SSL socket with remote hosts. 我有一个java应用程序,它创建与远程主机的SSL套接字。 I want to employ threads to hasten the process. 我想使用线程来加速这个过程。

I want the maximum possible utilization that does not affect the program performance. 我希望最大可能的利用率不会影响程序性能。 How can I decide the suitable number of threads to use? 如何确定要使用的合适线程数? After running the following line: Runtime.getRuntime().availableProcessors(); 运行以下行后: Runtime.getRuntime().availableProcessors(); I got 4. My processor is an Intel core i7, with 8 GB RAM. 我得到4.我的处理器是英特尔酷睿i7,内存为8 GB。

If you have 4 cores, then in theory you should have exactly four worker threads going at any given time for maximum optimization. 如果你有4个内核,那么从理论上讲,你应该在任何给定的时间内准确地有四个工作线程进行最大程度的优化。 Unfortunately what happens in theory never happens in practice. 不幸的是,理论上发生的事情在实践中从未发 You may have worker threads who, for whatever reason, have significant amounts of downtime. 您可能有工作线程,无论出于何种原因,他们都有大量的停机时间。 Perhaps they're hitting the web for more data, or reading from a disk, much of which is just waiting and not utilizing the cpu. 也许他们正在网上寻找更多数据,或从磁盘读取,其中大部分只是等待而不是使用cpu。

Depending on how much waiting you're doing, you'll want to bump up the number. 根据你正在做多少等待,你会想要提高数字。 The costs to increasing the number of threads is that you'll have more context switching and competition for resources. 增加线程数的成本是您将有更多的上下文切换和资源竞争。 The benefits are that you'll have another thread ready to work in case one of the other threads decides it has to break for something. 好处是你将有另一个线程准备工作,以防其他线程之一决定它必须中断某些东西。

Your best bet is to set it to something (let's start with 4) and work your way up. 你最好的选择是将它设置为某种东西(让我们从4开始)并逐步提升。 Profile your code with each setting, and see if your benchmarks go up or down. 使用每个设置配置代码,并查看基准测试是否上升或下降。 You should soon see a pattern and a break-even point. 你很快就会看到一个模式和一个收支平衡点。

When it comes to optimization, you can theorize all you want about what should be the fastest, but you won't beat actually running and timing your code to truly answer this question. 在优化方面,您可以将所有想要的内容理论化为最快的,但是您不会打败实际运行时间并对代码进行计时以真正回答这个问题。

As DarthVader said, you can use a ThreadPool ( CachedThreadPool ). 正如DarthVader所说,你可以使用ThreadPool( CachedThreadPool )。 With this construct you don't have to specify a concrete number of threads. 使用此构造,您不必指定具体数量的线程。

From the oracle site: 来自oracle网站:

  • The newCachedThreadPool method creates an executor with an expandable thread pool. newCachedThreadPool方法使用可扩展线程池创建执行程序。 This executor is suitable for applications that launch many short-lived tasks. 此执行程序适用于启动许多短期任务的应用程序。

Maybe thats what you are looking for. 也许这就是你要找的东西。 About the number of cores is hard to say. 关于核心数量很难说。 You have 4 hyperthreading cores, at least one core you should leave for your OS. 您有4个超线程核心,至少应该为您的操作系统留出一个核心。 i would say 4-6 Threads. 我会说4-6线程。

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

相关问题 如何确定应用程序的线程数? - How to decide on number of threads for an application? 如何为 Java 应用程序确定合适的 TLABSIZE 设置? - How do I decide on a suitable TLABSIZE setting for a Java application? 您是否应该在Java中创建无限数量的线程? - Should you create an unlimited number of threads in Java? 如何在 Java 中创建动态线程 - How To Create Dynamic Threads in Java 如何在Java中创建这三个线程 - How to create these three threads in Java 如何确定is-a或instanceof关系在给定情况下是否合适? - How to decide whether is-a or instanceof relationship is suitable in a given situation? 如何在不轮询线程状态的情况下计算 java 中完成的线程数 - How to Count the number of threads completed in java without polling for the threads status 如果java中的线程数未知,如何通知所有线程 - how to notify all the threads if there is unknown number of threads in java 有人可以告诉我如何在Java中创建线程组中n个线程的数组吗? - Can someone tell me how to create in java an array of n number of threads in a thread group? Java如何限制作用在方法上的线程数 - Java how to limit number of threads acting on method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM