• Post Reply Bookmark Topic Watch Topic
  • New Topic

ReentrantLock Monitor  RSS feed

 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a piece of road which is a critical section. Only one car at a time can use it. In my monitor-class I have one enter()-method and one leave()-method. A boolean variable "occupied" is initially set to false. The lock-method confuses me.

Scenario: Car nr1 enters the critical section first. It locks the lock and sets occupied to true. Then it unlocks the lock and leaves the enter()-method. Now car nr1 one is driving in the critical section.
Then car nr2 tries to enter the critical-section. It invokes enter() and locks the lock. Then it has to wait(while-loop) for car nr1 to set the occupied-variable to false and to signal in order to continue.
What confuses me is that car nr2 has the lock while waiting for car nr1 to leave. How can car nr1 enter the leave()-method and signal when car nr2 has the lock?

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Condition.await() releases the lock while the thread is waiting. This means that at this point, your program should be in a well defined state (that is, await() can not be in the middle of what you consider to be an atomic operation).

After the thread wakes up from await(), it will have acquired the lock once more.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:Scenario: Car nr1 enters the critical section first. It locks the lock and sets occupied to true. Then it unlocks the lock and leaves the enter()-method. Now car nr1 one is driving in the critical section.
Then car nr2 tries to enter the critical-section. It invokes enter() and locks the lock. Then it has to wait(while-loop) for car nr1 to set the occupied-variable to false and to signal in order to continue.
What confuses me is that car nr2 has the lock while waiting for car nr1 to leave. How can car nr1 enter the leave()-method and signal when car nr2 has the lock?

Because there's nothing to prevent it from doing so. Personally, I think what you've written might be overkill, because you appear to use the lock to set another "lock" (your 'occupied' field). The normal pattern for locks (from the docs) is:(and that try...finally block is essential)

So if I was going to do it, I think I'd just have a driveCriticalSection() method (or possibly even a CriticalSection class with a single driveThrough() method), rather than two separate ones.

I'm also not quite sure what your Condition is there for. I assume it's there to emulate some sort of traffic signal, but I have a feeling that you could still do that with a single lock - but maybe not.
Perhaps you'd care to explain what you're trying to get the code to do.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Perhaps you'd care to explain what you're trying to get the code to do.

Actually, I think I may have worked it out now - and I now understand the reason for your 'occupied' field and the two methods.

Suggestion: Have a look at the example in the Condition docs (java.util.concurrent.locks.Condition ←click). It's not exactly what you want, but it's not far off. You might also find that an 'inSection' counter works better than a simple 'occupied' flag.

HIH

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if the junction can be used by one car at a time, a boolean flag would be perfect.

I think while (occupied) junctionEmpty.await(); reads more easily than using a counter.

And Winston is absolutely correct in that you should wrap your critical section in a try-finally statement.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, if the junction can be used by one car at a time, a boolean flag would be perfect.

And that's what (I think) I finally worked out. I think this is supposed to emulate a section of road that - for example - has road works on it.

So what's likely to happen is that you have signals at each end that allow cars into the section, but they need to work independently, so that cars from the other end can't enter until all the cars currently in the section have cleared through. Not absolutely sure, but it's what makes sense to me; and it's why I suggested a count.

Could be completely out to lunch though.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!