简体   繁体   中英

Does java code automatically utilize multiple processor cores if available

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?

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.

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.

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.

As far as I know, the JVM does take advantage if the option exists. If you run the profiler tool: java/jdk[version]/bin/jvisualvm.exe you can observe how even a normal, "singled threaded" program makes use of multiple threads. I can't recall offhand if the profiler shows which cores are used for the given threads, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM