aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread behaviour in multiple CPU/Processor Environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread behaviour in multiple CPU/Processor Environment" Watch "Thread behaviour in multiple CPU/Processor Environment" New topic
Author

Thread behaviour in multiple CPU/Processor Environment

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Hi,

When a jvm invokes multiple threads in multiple cpu's environment, do threads share CPU's or they run only in the CPU where JVM is running

Also,Is there any way to see/print in which JVM a java thread is running,?

Thanks,
Don...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18908
    
    8

Don Redd wrote:the CPU where JVM is running


Are you sure this is a meaningful concept?
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Not sure though, but the Actual question comes to my mind from below scenario;

Lets say, I have a System with 64 processors(CPU's), I started an instance of JETTY web sever, where my web app is running, My web app creates Java Threads internally and I want to know whether the threads use more than one processor to run or not to achieve real parallelism. ( As,am seeing not a great improvement in terms of TIME with increase in thread count, I GUESS, its using single processor/CPU only).


Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Don Redd wrote:Not sure though, but the Actual question comes to my mind from below scenario;

Lets say, I have a System with 64 processors(CPU's), I started an instance of JETTY web sever, where my web app is running, My web app creates Java Threads internally and I want to know whether the threads use more than one processor to run or not to achieve real parallelism. ( As,am seeing not a great improvement in terms of TIME with increase in thread count, I GUESS, its using single processor/CPU only).



Well, in that case, technically speaking, its JVM's call about how to allocate threads - it can be one thread per CPU, or two threads per CPU, or 5 threads on one CPU, and 2 threads on another, or any other combination.

Now, distributing threads evenly over CPU is one thing, and witnessing significant improvement in application due to multiple threads is another thing. Its not always linear - like 1 thread does an operation in 1 second, hence 1000 threads would finish it in 1 millisecond.
There are other factors like how is speed-bottleneck (e.g. if all thread starts doing disk I/O, then even 64 threads will make the operation miserably slow than a single thread operation), how is result-merging time (e.g. is it really worth to divide a task in 100 small task, executing those separately, and then merging the result?) and so on.

However, even beyond everything, if you want to guarantee the true parallelism, I would recommend to take a look at fork-join framework introduced in JDK 7. There is a facility to allocate separate thread on separate CPU.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82


This is helpful

Thanks Anayonkar,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18908
    
    8

I would suggest that a web application isn't a good way to test the performance of thread allocation unless you can get it heavily loaded, by sending it a heavy volume of requests. Otherwise the response time is going to be dominated by network transmission time, and any gain from different thread allocation behaviour is going to be far too small to notice.
 
 
subject: Thread behaviour in multiple CPU/Processor Environment