Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One question about the example "Minister of church" from Max's book

 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

In Max's book at pages 82-84 there is one example with Minister of church.
And in the class CheckCollections's method run() there is following code:



My question is: why is the notification for all threads, that the collectionplate is not any more locked, not be used at the end of the synchronize block?


Thanks a lot for your explanation!
Regards,
Olena
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Olena,

Ooops - I just spent 15 minutes working out a really good answer, then suddenly realised I was answering a question you didn't ask .

In that example code, there is only ever one thread that goes into Wait state - the minister counting the money that has been collected. Once the $100 has been collected the minister is happy and the application (Church service?) can continue.

Since there is only one thread that is ever in Wait state, there is no need for the minister to call notifyAll() - there is nobody to notify.

Does this make sense?

Regards, Andrew
 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Andrew,

thank you very much for your question. I got it! You are right, it's only one thread, I see it now.
I don't have a lot of experience with threads and sometimes I need a lot of time to understand these very easy things.

But I think, that you spent 15 minutes to answer my second question
And this is about the lock and unlock method with the notifyAll(). Am I right?
Some days ago I read one old topic about lock method and calling the notifyAll in this method. I found this
example with the minister and thought, that it can be some correspondence with the lock method without notifyAll() and this example. But now I see I was wrong.

Today I found the answer to my second question.
We don't need to put at the end of the synchronized block in lock method something like this:
lockedRecords.notifyAll();

because it is used to notify all waiting threads that the record was unlocked. But in the locking method the record will not be unlocked, but locked.
Am I right?

Olena
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Olena
We don't need to put at the end of the synchronized block in lock method something like this:
lockedRecords.notifyAll();

because it is used to notify all waiting threads that the record was unlocked. But in the locking method the record will not be unlocked, but locked.
Am I right?


Yep, that's right.

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic