*
The moose likes Threads and Synchronization and the fly likes Threading question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threading question" Watch "Threading question" New topic
Author

Threading question

john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
I have two simple programs that do the same thing. One threaded one not. I'm doing something wrong in the threaded one as it takes , on average , 4 times longer to do the same ting as the unthreaded version. I'm running two threads on a macbook pro. The activiy monitor show both cores going really hard(95%). Can someone point me in the right direction?
Here is the threaded version.
Main:

NumberGenerator:


here is the unthreaded version:


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

I am by no means an expert on this, so everything I say may be wrong...

why do you think making it multi-threaded will speed things up? threads are not a magic bullet. My understanding is that they can help if your CPU is idle a lot while waiting for things - a message, a printer to be available, user input, etc. But if all your program does is use the CPU, using threads can slow it down, as it now has to manage all the thread stuff in addition to doing the work.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33

why do you think making it multi-threaded will speed things up?


Because I have a two core machine, I can have two threads running at the same time. The activity monitor on the mac actually shows a difference between the tow programs. I've tried this on a 16 core machine as well, It is also slower running threaded v. unthreaded.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

Because multi-threading takes extra overhead, and there's no way the task you're performing in your example is going to justify the overhead of more than one thread.

You should strive to make as much as possible run in a single thread, because it significantly simplifies your program.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33


Because multi-threading takes extra overhead, and there's no way the task you're performing in your example is going to justify the overhead of more than one thread.



Really?

The unthreaded version takes around 5 minutes to run and drives the cpu at about 50 %.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Well, Math.random() could be a source of a lot of contention in this scenario, I guess.
If you supply each thread with its only random generator (java.util.Random) I believe that would cut the synchronization overhead.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33

Well, Math.random() could be a source of a lot of contention in this scenario, I guess.
If you supply each thread with its only random generator (java.util.Random) I believe that would cut the synchronization overhead.


That's it!. Just changed the run(). and now it finishes in seconds. Thanks.

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

The increment of the static numberOfMatches member isn't thread-safe though.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
I think I know how to fix that...
Thanks.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Feel free to share ;)
Also, yielding inside the loop would increase fairness.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
To make it thread safe i think I could make a method like:


In this case would increasing fairness increase overall speed?

This is just a Rube Goldbergian way to generate lottery numbers and learn about threads.

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

john sayeau wrote:To make it thread safe i think I could make a method like:


In this case would increasing fairness increase overall speed?

This is just a Rube Goldbergian way to generate lottery numbers and learn about threads.



Afraid that's still not safe.
You're now locking on the intrinsic lock associated with the NumberGenerator instance.
Of which there are two. The field is static, which means its not associated with any particular instance.

Increasing fairness would not do anything to increase overall speed in this scenario.
john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
I'll figure it out. Lots to learn. Just starting with Threading.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Java 5 introduced a lot of new classes that tackle problems related to concurrency. They can be found in the java.util.concurrent package. One of the classes which can be found in the java.util.concurrent.atomic package can be used to solve one of your issues here.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
No longer a "beginning" topic. Moving discussion to our threads forum.
 
jQuery in Action, 2nd edition
 
subject: Threading question