问题描述
我在Java客户端服务器应用程序中观察到一个非常奇怪的问题.我正在以每秒80个请求的速度向服务器发送以下可运行对象.线程池保持池大小等于请求速率,即池中大约80个线程.我的笔记本电脑是英特尔酷睿i5-3230M 双核(Windows向我展示了4个处理器) .奇怪的是吞吐量(每秒完成的作业数)也为80.我无法理解. 4个处理器和80个线程如何在一秒钟内完成100毫秒的80个工作?那就是:
I am observing a very strange problem in a java client server application. I am sending following Runnable objects to the server at 80 requests per second. The thread pool keeps pool size equal to the request rate i.e. approximately 80 threads in the pool. My laptop is intel Core i5-3230M dual core(Windows show me 4 processor). Strange thing is that the Throughput(jos completed per second) is also 80. I could not understand this. How 4 processors and 80 threads are completing 80 jobs of 100 milliseconds in one second ? That is:
Processors=4
Request rate=80
Thread pool size=80
Each job service time=100milliseconds.
Throughput=80 How?
我期望吞吐量= 40,因为每个处理器应该在1秒内大约完成10个作业,所以4个处理器应该给出吞吐量= 40,但它是80?笔记本电脑规格链接说
I was expecting throughput=40 because each processor should approximately complete 10 jobs in 1 second so 4 processors should give throughput=40 but it is 80 ? Laptop specification link says
这是否意味着8个线程可以同时运行2个内核?
Does this means 8 threads can run at the same time b 2 cores?
public class CpuBoundJob implements Runnable {
public void run() {
long startTime = System.nanoTime();
while ((System.nanoTime() - startTime) < (100)*1000000L) {}
}
}
推荐答案
您编写的任务运行的时间是固定的,而不是固定的时间.这意味着无论您拥有多少CPU,它们都应始终以固定的速率完成.您可以让他们睡100毫秒.
You have written tasks which run for a fixed amount of time, not a fix amount of work. This means they should always complete at a fixed rate regardless of the number of CPUs you have. You could just have them sleep for 100 ms.
您的计算机一直在运行的线程多于您所拥有的进程.操作系统使用调度来停止和开始运行线程(比您所看到的更快),以使它们一次都运行的错觉,但它们却并不能(如果您考虑过,就永远不可能)
Your computer is running far more threads than you have processes all the time. The OS uses scheduling to stop and start running thread (faster than you can see) to give the illusion they all running at once but they are not and cannot (never could if you think about it)
这意味着它的两个内核都具有超线程,从而允许处理器运行多达四个线程而无需上下文切换(如上所述)
Its means it's two cores have hyper threading allowing the processor to running up to four threads without context switching (as mentioned above)
提到的i5
具有2个内核,它声明时支持4个线程.
The i5
mentioned has 2 cores it supports 4 threads as it states.
这篇关于Java线程池吞吐量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!