Results 1 to 5 of 5
  1. #1
    centenial is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default threading execution time question

    Hey everyone,

    Let me state up front that this is a homework assignment.

    I'm supposed to take a simple task (flipping a coin n number of times) and use threading to increase the execution speed of the program. The basic idea is that to flip a coin 1,000,000 times, you could launch n threads and have it execute approx. n times as fast.

    I've been working on it for a few days. The code is complete in that the task gets divided up and runs on multiple threads. However, the execution time doesn't nearly reflect what I would expect it to be.

    For example, flipping 10,000,000 "coins" takes:

    ~700ms using 1 thread.
    ~980ms using 2 threads.
    ~920ms using 4 threads.

    I would expect that the execution time would decrease as I add more threads. This time difference is magnified as I increase the number of coin flips. For example, flipping 100,000,000 "coins" takes:

    ~7 seconds using 1 thread.
    ~10 seconds using 2 threads.

    Does this make sense?

    I appreciate your time and any thoughts you might have. Thanks!
    Last edited by centenial; 09-09-2010 at 03:52 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I would expect that the execution time would decrease as I add more threads.
    If your PC has more cores or processors to handle the extra threads, then you will get an overlap in the processing times required/used by the program. If all the threads are on one core/processor then the threads are executed serially on the one core with the overhead of switching between threads added on and so would take more time.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

  4. #4
    centenial is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks, okay, that makes sense. The machine that I'm running it on has an Intel Core 2 Duo. When I run the program on 1 thread, I see 100% of the first core being used. When I run the program using 2 threads, I see 100% of both cores being used. So, I think that it's using both cores... but the timings don't reflect that.

  5. #5
    centenial is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    In case anyone was having the same problem, copeg on javaprogrammingforums.com gave me great advice:

    Quote Originally Posted by copeg
    You are calling a static function Math.random within several threads. This function is most-likely thread-safe although I don't have time right not to verify this. So basically, when more than one thread is created, when each thread tries this function - if another thread is using it - it will have to wait until the lock is released. Suggestion, create an instance variable of Random in your coin flip class. This should result in expected behavior if the above is correct
    I changed my coin toss method to use the nextBoolean() method in the Random class and it fixed the problem.

Similar Threads

  1. Question on dependency execution
    By java_new in forum New To Java
    Replies: 3
    Last Post: 05-02-2010, 07:35 AM
  2. Need help with Process execution time display
    By murali18 in forum New To Java
    Replies: 5
    Last Post: 03-18-2010, 11:19 PM
  3. Replies: 25
    Last Post: 06-28-2008, 05:08 PM
  4. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 09:51 AM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •