在java中,应用程序中CPU核心数与线程数之间的关系是什么? [英] What is the relationship between number of CPU cores and number of threads in an app in java?

查看:260
本文介绍了在java中,应用程序中CPU核心数与线程数之间的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java 多线程编程的新手。我想到的问题是我可以根据 CPU内核的数量运行多少个线程。如果我运行的线程超过 CPU核心,那么它将成为机器运行应用程序的开销。例如,当我们的服务器机器有一个运行2个线程的服务器软件(主线程+开发人员线程)时,当更多的并发客户端与服务器建立套接字连接时,它是否会成为服务器的开销?

I'm new to java multi-threaded programming. The question that has came to my mind is that how many threads can I run according to the number of my CPU cores. and if I run threads more than CPU cores will it be an overhead for the machine to run the app. for example when we have a server machine which has a server software that run 2 threads(main thread + developer thread), will it be an overhead for the server when more simultaneous clients make socket connections to the server or not?

谢谢。

推荐答案

系统可以执行的线程数同时(当然)与系统中的核心数相同。

The number of threads a system can execute simultaneously is (of course) identical to the number of cores in the system.

系统中可存在的线程数受可用内存的限制(每个线程需要一个堆栈和操作系统用来管理线程的结构),并且可能存在操作系统允许的线程数限制(这取决于操作系统架构,某些操作系统可能会使用固定大小的表和一次它可以创建完整的线程。)

The number of threads that can exist on the system is limited by the available memory (each thread requires a stack and a structure used by the OS to manage the thread), and possibly there is a limitation how many threads the OS allows (this depends on the OS architecture, some OS' may use a fixed size table and once its full no more threads can be created).

通常,今天的计算机可以处理数百到数千个线程。

Commonly, todays computers can handle hundreds to thousands of threads.

系统中存在使用比核更多线程的原因m是:大多数线程将不可避免地花费大量时间等待来处理某些事件(例如:字处理器等待用户在键盘上键入)。操作系统管理它以这种方式等待的线程不消耗CPU时间。

The reason why more threads are used than cores exist in the system is: Most threads will inevitably spend much of their time waiting for some event (example: word processor waiting for user to type on keyboard). The OS manages it that threads that wait in such a manner do not consume CPU time.

这篇关于在java中,应用程序中CPU核心数与线程数之间的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆