Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HorseRace example from Thinking in Java

 
Vladimir Mokrozub
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vladimir Mokrozub
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic