問題描述
如果給定處理器上有多個內核可用,當 JVM 運行用戶編寫的 java 代碼時,它們會自動使用嗎?還是必須專門編寫代碼才能利用多核?
If multiple cores are available on a given processor, will they be utilized automatically when JVM runs a java code written by user? Or the code will have to be specifically written to take advantage of multi-core?
我的意思是,我們是否必須為 JVM 創建不同的代碼才能在運行時利用多個內核,例如通過程序員在用戶代碼中創建多個線程?并且說如果我們在 java 代碼中不使用多線程,那么無論有多少內核可用,JVM 都將無法利用多個內核.可能是這種情況——但我不確定.
I mean, do we have to create the code any differently for JVM to be able to take advantage of multiple cores while running it, say by means of the programmer creating multiple threads in the user code? And say if we don't use multi-threading in the java code, JVM won't be able to take advantage of multiple cores no matter how many cores are available. This might probably be the case -- but I am not sure.
推薦答案
在 java 中可以通過創建單獨的線程來利用多核.與早期的 JVM 實現不同,現代實現創建了 OS 可以識別的本機線程,因此每個可用的處理器都可以分配給運行 java 程序中創建的每個線程.但是,這并不意味著您將始終獲得多處理(多核利用)的優勢. 這取決于問題以及解決方案的設計方式.設計不當的解決方案無法利用多核處理.
It's possible in java to take advantage of multi-core utilization, by creating separate threads. Unlike earlier implementations of JVM, modern implementations create native threads, which OS can recognize and thus, each available processor can be allocated to each thread created in running java program. But, that doesn't mean that you will always get the advantage of multiprocessing(multi-core utilization). It depends upon the problem and moreover, the way solution is designed. Improperly designed solutions doesn't take the advantage of multi-core processing.
注意:- 我的建議是,必須設計高度計算密集型作業,同時牢記并行計算(多核利用)架構.現代 CPU 已達到提高 CPU 時鐘頻率的最大極限.因此,像英特爾這樣的公司正在設計具有多核的 CPU.因此,開發人員有責任編寫軟件以利用處理器的多核能力.
NOTE :- My suggestion is, Highly computation intensive jobs have to be designed keeping parallel-computing(multi-core utilization) architecture in mind. Modern CPUs have reached the maximum limit of increasing CPU clock frequency. Thus, companies like Intel are designing CPUs which have multi-cores. Thus, It's the responsiblity of developer to write software to take advantage of multi-core abilities of processor.
這篇關于java 代碼是否會自動利用多個處理器內核(如果可用)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!