my dog learned polymorphism*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Review lock/unlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Review lock/unlock" Watch "NX: Review lock/unlock" New topic
Author

NX: Review lock/unlock

frank sun
Ranch Hand

Joined: Apr 11, 2003
Posts: 102
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

Joined: Apr 11, 2003
Posts: 102
Please help to comment this question!
Frank
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
frank sun
Ranch Hand

Joined: Apr 11, 2003
Posts: 102
Thanks for your reply. I think you already made me clear about this issue.
Frank
 
jQuery in Action, 2nd edition
 
subject: NX: Review lock/unlock
 
Similar Threads
Need your help -- IllegalMonitorStateException
notify main thread
Single table / Simple Locking - WeakHashMap vs WeakReferences
Thread Communication using wait() and notify()
I want to execution of a Threads in sequence