Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Review lock/unlock

 
frank sun
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got one questions about the sychronize when multi-users accessing the data file at the same time. that is, the method create() and delete() need to update the total record amount after the excecution, if those 2 methods are executing at the same time, will the result of recordCount be wrong? the below is sample code.
private boolean locking=false;
private synchronized incRecordCount(){
while(locking) {
wait();
}
locking=true;
++recordCount;
locking=false;
}
private synchronized decRecordCount(){
while(locking) {
wait();
}
locking=true;
--recordCount;
locking=false;
}
Assuming that both of them start to execute, and both find the flag, locking, is true, then one increase by 1 while the other one descrease by one at the same time.
Is my understanding right?
Please comment it.
Appreciation!
Frank
 
frank sun
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help to comment this question!
Frank
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming two threads are executing in the same object, nothing is going to "happen at the same time" because both methods are synchronized.
In fact nothing is going to happen until something does a notify/notifyAll call on the same object.
To get answers to this type of problem why don't you post it in the SCJP Certification? They eat this stuff for breakfast (But delete it here afterwards because of cross-posting)
 
frank sun
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I think you already made me clear about this issue.
Frank
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic