• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 CyclicBarriers , why?

 
Ranch Hand
Posts: 101
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, in OCP book we have this code :




I was wondering if we cannot just use one CyclicBarrier instead of initialising 2 (see c1, c2)

Thanks

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clue might be in the difference the two CyclicBarriers were created on lines 17 and 18: The first one does not have an action while the second one does.

Have you tried to see what happens if you only use one? Have you tried to see what happens if you use one instead of the other and vice versa?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just by guessing at the code... Perhaps, it is not possible to start cleaning the pen until all animals have been removed? Otherwise the animals may be hurt? Perhaps, it is not possible to add animals to the pen until the cleaning process have been completed? Again, otherwise the animals may be hurt?

This implies three stages, and hence, two barriers. Anyway, how would you propose using a single barrier for three stages?

Henry
 
A. Aka
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with one CyclicBarrier and it works, so I was wondering why it was necessary to have 2 distinct CyclicBarriers
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Just by guessing at the code... Perhaps, it is not possible to start cleaning the pen until all animals have been removed? Otherwise the animals may be hurt? Perhaps, it is not possible to add animals to the pen until the cleaning process have been completed? Again, otherwise the animals may be hurt?


It's a lion pen. I'd be more concerned with the people cleaning the pen to be hurt!

A. Aka wrote:I tried with one CyclicBarrier and it works, so I was wondering why it was necessary to have 2 distinct CyclicBarriers


How do you know? With threads, it is easy to have something happen by coincidence and appear to work.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A. Aka wrote:I tried with one CyclicBarrier and it works, so I was wondering why it was necessary to have 2 distinct CyclicBarriers


We often say "'It doesn't work' is useless" around here but in this case, "It works" is pretty useless as well. Show us the code that you tried so that we can see what you claim works. As Jeanne says, it's probably the case that it just "appears" to work but it's not actually doing so as intended by the original code.
 
A. Aka
Ranch Hand
Posts: 101
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this :



where I am not using the second CyclicBarrier, i did it so i can understand what s going on and I was surprised by the result!

KR,
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok. I thought you were proposing only one await() rather than re-using the Cyclic Barrier. Your code does work since the barriers are the same size and not executing in parallel.

There's two reasons we used separate barriers:
1) Clarity
2) We wanted to print out a message that the pen was cleaned. The second barrier takes care of this for us.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the goal of clarity was not quite achieved then.

I'd imagine you could put some more relatable context around that example (which you might already have, I don't know, I don't have the book). The scenario would be that you have X number of lions in the pen and you have Y number of lionkeepers, where both X and Y are more that 1 and they are not necessarily equal (although they could be). The process of cleaning the lion's pen would be remove all lions, clean the pen, return all lions.

The constraints would be something like:
1. Two keepers are required to remove one lion.
2. All lions must be removed from the pen before any keeper can start cleaning.
3. One keeper can clean only a fraction of the pen, say 1/4th, so to clean the whole pen requires 4 keepers.
4. Only two keepers are needed to return all the lions to the pen.

That's the kind of twist I would probably experiment with to try to understand CyclicBarriers more thoroughly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic