Diff. between running java code in a single processor vs multiprocessor
Hi,
This is a general question and I tried to find answer for it, but couldn't find so came here as usual.
When designing a new Java project do we need to consider whether the application will be running in a single processor/multi processor. Does our design affect assigning thread to different processors? if so what the steps that we need to take, so that my application makes full use of multiprocessor. What is the difference in desinging application for single processor and multi processsor? Recently we had a problem in which our application makes use of only 2 processor out of 16 and I have no answer for it.
Thanks.
# 1
It's basically the same as it would be in any other programming language.
If you are unfamiliar with multithreaded applications, I suggest you buy a book or take a course to understand what the implications, benefits and drawbacks are of multithreading.
# 2
you are more or less asking to summarize a good book on the theme Threading :/
To answer your question shortly:
1st question:
whether you should consider multi-threading during design depends on the goals you try to achieve. I.e: if you're goal is to build a simple command line tool for converting unix text files to windows text files then considering building it with multiple threads seems overkill to me. In general I personally would say yes to this question, but if you have a team of none-expert-programmers you should probably stay with the single threaded approach: Be aware that the future will be multi threaded!
2nd question:
I'm not so good with designs .. I think it's hard to correctly put multi threading into a single model especially with the tools available at this moment.
Following questions:
On every data/object you should ask yourself: "is their only one thread involved with this particular data/instance or are multiple threads reading/writing(=modifying) the instance"
It's hard to believe that a Java program occupies only 2 CPUs. Probably you only saw 2 active CPUs that's because all other Java threads were probably waiting. Seems more to me you wrote an application not taking in consideration that there could be more the 1 processor.
# 3
I have some good amount of experience working with multi threaded java program and i know about the overhead involved and overkill in multi threaded programs.
My question is :
Will design vary depending whether we deploy it in a multi-processor / singe processor CPU ?
If it's the job of the operating system to assign threads to different processor, then it doesn't matter whether I design keeping in mind single processor or multi processor CPU? OS will intelligently give different threads to different processor to make use of multiple processors right?
# 4
> I have some good amount of experience working with
> multi threaded java program and i know about the
> overhead involved and overkill in multi threaded
> programs.
>
let's assume that for the moment (many beliefs held about MT and MP computing are wrong, including possibly some or all of my own).
> Will design vary depending whether we deploy it in a
> multi-processor / singe processor CPU ?
>
If it was correctly implemented before, no.
But keep in mind that instead of receiving subsequent timslices threads can in an MP environment run truely in parallel.
If threads depend on the timing of other threads (ugly but we all know it happens) things can go wrong.
Of course as with all MT issues they tend to go wrong only after the product has passed all tests and has shipped to customers.
> If it's the job of the operating system to assign
> threads to different processor, then it doesn't
> matter whether I design keeping in mind single
> processor or multi processor CPU? OS will
Read above. It SHOULD NOT matter, if your design is good.
> intelligently give different threads to different
> processor to make use of multiple processors right?
Either the OS or the hardware. In an MP system there may well be dedicated hardware to do CPU allocation rather than it being handled by the operating system.
# 5
> If it's the job of the operating system to assign threads to different processor, then it doesn't matter whether I design keeping in mind single processor or multi processor CPU? OS will intelligently give different threads to different processor to make use of multiple processors right?
Code that doesn't make use of Threads or a framework capable of multithreading, can not be split by the system.
long k = 1;
for(int i=0; i<200000000; i++) {
k = k+(k-1);
}
above code can not be split by the system but could run for a few minutes occupying one CPU RUNNING 100% even if your machine has 16 CPUs.