• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

HorseRace example from Thinking in Java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic