• 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

NX: Review lock/unlock

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help to comment this question!
Frank
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. I think you already made me clear about this issue.
Frank
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic