Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question

closed as too broad by Jimmy Hoffa, Robert Harvey, Snowman, GlenH7, gnat Oct 15 '14 at 3:53

There 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.

    
there is but it's advanced and you shouldn't bother with it as it doesn't actually make a difference unless you are micro-optimizing –  ratchet freak Oct 14 '14 at 22:14
    
This is a specific implementation question about Java (I suspect it's also too broad altogether because the decisions about which cores any thread runs on are strongly tied to the underlying OS, and particular chip platform which entails way more variables than anybody can describe in an answer) voting to send to SO –  Jimmy Hoffa Oct 14 '14 at 22:36
    
Why don't you measure this yourself? At least in Windows it is easy to set affinity of a process to simulate different scenarios with a specially-crafted test program. –  Snowman Oct 14 '14 at 23:41
    
I am sure there are people who already measured this stuff and can tell what the results are. Why don't you keep this comment for you? :) –  Кристиян Кацаров Oct 15 '14 at 8:17

2 Answers 2

Will be there a difference?

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:

  • If there is shared data, then synchronization overheads may reduce the speedup.

  • If the computation uses significant memory, then contention for physical memory bandwidth may cause slow down.

  • If there is I/O, that will typically slow down the thread performing it ... to the effective speed of the device or network involved the data transfer.

  • High rates of memory allocation may lead to slowdowns across the board. The precise effects are complicated, but adding active threads / cores could impact on throughput.

Note that these things are almost impossible to predict without detailed knowledge of the application, etcetera.


... and what does Java/OS do in backend with different threads?

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:

I am sure there are people who already measured this stuff and can tell what the results are.

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.)

share|improve this answer

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.

share|improve this answer
    
That was my question, the exact dependency or at leat what Java/ the OS do :) –  Кристиян Кацаров Oct 14 '14 at 22:29
    
It makes threads. What are you asking, exactly? –  Robert Harvey Oct 14 '14 at 22:41
    
What does windows/linux/mac do for example in this case ? Or what is best practice, should we worry about this, how we can optimize it :) –  Кристиян Кацаров Oct 14 '14 at 22:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.