Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: One process open multiple threads on multiple CPUs

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default One process open multiple threads on multiple CPUs

    I am using the code below to open up multiple threads (20+).
    However, it seems that all the threads are running on 1 CPU when I do have 4 cores(according to task manager).
    I am guessing there is a limitation on where my threads can be run.
    In other word, threads can only be open up on the CPU where the original process started.
    Obviously something have gone wrong some where.
    Anybody got some suggestions?
    Thanks a lot in advance

    Additional information on platform:
    I am using Eclipse to code.
    Have tried to run on both windows 7 and XP, none works.

    Runnable runnable = new XThread();
    Thread thread = new Thread(runnable);
    thread.start();

    public class XThread implements Runnable
    {
    public void run()
    {
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: One process open multiple threads on multiple CPUs

    I am guessing there is a limitation on where my threads can be run.
    In other word, threads can only be open up on the CPU where the original process started.
    Did you test your assumption? Write an SSCCE with several threads that work at full capacity for a particular duration and watch your CPU...based upon the code you posted (and please use the code tags) I'm not sure how you expect to see 20+ empty Runnable implementations to divide no work across your CPU's. And why 20+? That is a lot of overhead

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One process open multiple threads on multiple CPUs

    Quote Originally Posted by copeg View Post
    Did you test your assumption? Write an SSCCE with several threads that work at full capacity for a particular duration and watch your CPU...based upon the code you posted (and please use the code tags) I'm not sure how you expect to see 20+ empty Runnable implementations to divide no work across your CPU's. And why 20+? That is a lot of overhead
    1. I know they are running cause I saw 1 CPU is at max capacity all the time, while all other CPUs are not doing anything.
    2. Why 20+?
    Cause I notice a significant improvement of running speed when I increase the number of threads.
    The reason:
    I am processing many small files (30% of my 10000+ files are less than 5k; some of my big files could be up to 1M).
    Therefore, there is huge difference in terms of processing speed between the files(mainly due to the time it takes to load the file to my program).
    I don't want to open up 3 threads and wait for the big files to finish before opening up the small files.
    But this is not important, as if I open up 20+ threads, I should expect other CPUs to take some load.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: One process open multiple threads on multiple CPUs

    Like I said, an SSCCE. You can describe what you are doing as much as you like - code better lays the foundation for understanding.

    Threads have to be managed - gaining and releasing monitors with synchronization adds overhead...there is a sweet spot for number of threads, after which your improvement in optimization may actually take a hit - this is why I advise against 20+ threads. But I will leave it up to you decide what is optimal for your task at hand.

    Try running the following and analyze your CPU's, you may have to do it a few times - playing with LENGTH and MAX_THREAD values so that the work gets tough enough to show up in the task manager.

    /**
    * Thread demonstration. With the correct values for LENGTH and MAX_THREAD (OS dependent) one should see work distribute across all
    * CPU's during the course of running this script.
    */
    public class Test {
     
    	private static final int LENGTH = 1000000;
     
    	private static final int MAX_THREAD = 4;
     
    	private static class MyRunner implements Runnable{
    		public void run(){
    			for ( int i = 0; i < LENGTH; i++ ){
    				double d = Math.cos(Math.PI/1.23d);//do some random but semi-intesive work
    				for ( int j = 0; j < 20; j++ ){
    					d = Math.atan(d);
    					d /= Math.sin(d);
    				}
    			}
    		}
    	}
     
    	public static void main(String[] args){
    		Runnable[] runners = new Runnable[MAX_THREAD];
    		for ( int i = 0; i < MAX_THREAD; i++ ){
    			runners[i] = new MyRunner();
    			(new Thread(runners[i])).start();
    		}
    	}
    }

    With the right values of LENGTH and THREAD_COUNT, you should see the work distribute across all CPU's. The above is an SSCCE - if you consistently see your work being thrown onto a single CPU regardless of the number of threads or length of work performed, I would say this is support for your conclusion that your threads are running on a single CPU - and if this occurs post your OS.
    Last edited by copeg; December 9th, 2011 at 12:48 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One process open multiple threads on multiple CPUs

    First of all, thank you so much for the quick and detailed reply.
    Really appreciate it.

    And your code does occupy all 4 CPUs.
    I will do more testing to see why my code doesn't.
    Thanks,

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: One process open multiple threads on multiple CPUs

    I think I know what's going on now.
    Each thread takes very short amount of time to process.
    It is very hard to use up to full capacity.

    However, in this case, wouldn't I benefit from having more threads than CPUs?
    I understand switching between threads is expensive but having idle capacity should help right?
    Why am I only seeing a 5% increase in performance when I am running 20 threads instead of 5?

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: One process open multiple threads on multiple CPUs

    However, in this case, wouldn't I benefit from having more threads than CPUs?
    I understand switching between threads is expensive but having idle capacity should help right?
    Why am I only seeing a 5% increase in performance when I am running 20 threads instead of 5?
    Why not try it both ways and see? And given you have provided no code, I won't guess at your last question.

    For a situation such as this, I will typically create a thread queue: with several threads that pull their operations from a Queue containing Runnable implementations. If you've got 20+ separate things to do, these get placed into the queue for the threads to chew on. See http://en.wikipedia.org/wiki/Thread_pool_pattern

Similar Threads

  1. How to use multiple actionlisteners
    By pottsiex5 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 19th, 2011, 09:37 AM
  2. multiple jvms
    By gaikwaddeepali111 in forum Member Introductions
    Replies: 0
    Last Post: September 10th, 2011, 12:22 AM
  3. use multiple gui displays
    By jonwymore in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 10:11 AM
  4. Multiple Multishuffles
    By dino2dy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2010, 01:36 PM
  5. working with multiple threads
    By retsameht in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 9th, 2010, 01:36 PM