This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes HorseRace example from Thinking in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "HorseRace example from Thinking in Java" Watch "HorseRace example from Thinking in Java" New topic
Author

HorseRace example from Thinking in Java

Vladimir Mokrozub
Greenhorn

Joined: Aug 31, 2012
Posts: 12
Hello,

I have a question about a HorseRace example from Bruce Eckel's Thinking in Java.



The question is: why do we need to synchronize strides field in the Horse class? The only two threads that have access to it are Horse itself and Runnable in CyclicBarrier. And the documentation for CyclicBarrier says:
A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This barrier action is useful for updating shared-state before any of the parties continue.

As far as I understand, this means that when the last active Horse thread calls await and goes to sleep, CyclicBarrier runs its Runnable before any Horse threads are awakened so there will be no collision when accessing strides. Am I right?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi,
Welcome to the Ranch!

Your understanding is not right. You have quoted the CyclicBarrier API - it says "an optional Runnable command that is run once per barrier point" - what is your 'barrier point' here?


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Vladimir Mokrozub
Greenhorn

Joined: Aug 31, 2012
Posts: 12
Well, the barrier point is reached when each Horse makes one stride, following which they all goes into a blocking state and CyclicBarrier performs its action and then releases threads.
BTW, I've changed the code to make some observations:

Horse class:


CyclicBarrier:


Here is the output:
Thread 0, Thread 1, Thread 2, Thread 3, Thread 4, Thread 5, Thread 6, Action
Thread 6, Thread 2, Thread 4, Thread 5, Thread 1, Thread 0, Thread 3, Action
Thread 3, Thread 2, Thread 4, Thread 5, Thread 1, Thread 0, Thread 6, Action
Thread 6, Thread 3, Thread 2, Thread 4, Thread 5, Thread 1, Thread 0, Action
Thread 0, Thread 3, Thread 2, Thread 6, Thread 4, Thread 5, Thread 1, Action
Thread 1, Thread 0, Thread 3, Thread 2, Thread 4, Thread 5, Thread 6, Action
Thread 6, Thread 1, Thread 0, Thread 3, Thread 2, Thread 4, Thread 5, Action
...

Doesn't this prove that all threads are waiting at the barrier point until the CyclicBarrier completely executes it's action?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi,
I re-read your initial post. I think you are right. I was wrong in my earlier post. I initially thought a single object is shared by all the threads (which is not the case here).
And your understanding of CyclicBarrier is correct. I am not sure why the author has synchronized access to strides here...
Vladimir Mokrozub
Greenhorn

Joined: Aug 31, 2012
Posts: 12
OK, thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HorseRace example from Thinking in Java
 
Similar Threads
print out all possible permutations of the number?
reusing threads
Movement of user causes AI to lag
Animation in Swing
Threading odd/even