I have a simple question this time. If I write an java application with 4 Threads ( each thread does different work ), what will be the difference between single-core, dual core and quad core cpu ( imagine the cpu's have almost same speed, there is only difference in cores ). Will be there a difference and what does Java/OS do in backed with different threads ?
closed as too broad by Jimmy Hoffa, Robert Harvey, Snowman, GlenH7, gnat Oct 15 '14 at 3:53There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||||||
|
Yes. How much difference there is will be highly dependent on your application. In the unrealistic case where the application consists of N thread that do independent (i.e. no share data structures) CPU intensive computations with small memory working sets, no garbage generation and no I/O, then you can expect close to N-fold speedup with N real (i.e. not hyper-threaded) cores. But:
Note that these things are almost impossible to predict without detailed knowledge of the application, etcetera.
Typically, the OS attempts to map each active thread to a different core. If there are too many threads for cores, then it will attempt to "time-slice" at a fairly coarse grain. Switching threads involves a "context switch" and that is another source of slowdown. Also note that the OS has to share the cores between all processes in the system, not just your Java application's threads. You commented:
As I said, it is not practical to predict how an application is going to perform. Any measurements that other people have done (using artificial benchmarks) are unlikely to help you. (I'm sure people have tried to use general models, etc to predict performance of specific application ... but it just doesn't work.) |
||||
|
Assuming that your four threads are capable of utilizing the core at 100%, your load is approximately equal on all threads, and there is no threading overhead, the application will run twice as fast on the dual core as the single core, and four times as fast on the quad core. It never works out exactly that way, of course. How close your application comes to achieving that ideal outcome depends entirely on the design of your program: how well the threads utilize each processor core, and how much overhead is incurred maintaining and coordinating the threads, etc. |
|||||||||||||
|