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 5 of 5

Thread: java threading execution time question

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java 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; September 9th, 2010 at 09:36 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java threading execution time question


  3. #3
    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: java threading execution time question

    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
    Last edited by copeg; September 8th, 2010 at 08:19 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java threading execution time question

    It's also possible that you don't have a multi-core cpu. Only cpu's with multiple cores actually gain the speed boost for multi-threaded programs (note: there may still be other benefits of creating a multi-threaded application even though it will be run on a single core cpu). Otherwise you're just wasting time creating the threads, then your OS has to manage which thread gets CPU time, resulting in some slow down.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java threading execution time question

    Quote Originally Posted by copeg View Post
    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
    You're a lifesaver! Math.random() was the problem. I changed my program to use the nextBoolean() method in the random class, and I now see approx. a 2 times speed up when using 2 threads vs. 1. Thanks so much for your comments and advice.

Similar Threads

  1. problem in Threading !!!
    By roadies07 in forum Threads
    Replies: 4
    Last Post: July 14th, 2010, 10:21 AM
  2. Question on dependency execution
    By back2grave in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 2nd, 2010, 12:50 AM
  3. Java GUi program Execution
    By Rajan in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 08:48 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  5. Threading question....
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: September 2nd, 2009, 02:38 AM