Java确定最佳线程数的最佳方法

Java确定最佳线程数的最佳方法

问题描述:

我正在编写一个爬网程序,该爬网程序具有一组处理Web IO并处理每个网页的辅助线程.但是,我不确定如何找到系统的最佳线程数.监视线程性能并具体测量抖动等因素的最佳方法是什么?

I am writing a crawler which have a set number of worker threads that handle web IO and process each webpage. However, I am not sure how I can find the optimal number of threads for the system. What's the best way to monitor thread performance and concretely measure factors such as thrashing?

只需运行基准测试.您可以通过让线程定期向线程管理器报告其吞吐量来动态地执行此操作.管理器可以操纵线程数以探查性能如何随线程数变化并确定接近最佳的值.它也可以偶尔决定尝试看看是否可以找到一个新的最优值. (最佳数量可能会随环境条件的变化而变化,例如其他进程的总体CPU负载.)

Just run benchmarks. You can do this dynamically by having threads report their throughput on a periodic basis to a thread manager. The manager can manipulate the thread count to probe how the performance varies with thread count and settle on a near-optimal value. It can also occasionally decide to experiment to see if a new optimal can be found. (The optimal count may vary with varying environmental conditions like overall CPU load from other processes.)