Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Monkhouse book question: DvdFileAccess.persistDvd()

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
As you can see, my question is in the code.
Isn't it better to hold the write lock until the whole writing process ends? or the scenario that i'm worring about is handled somewhere else?
Thank you for reading.
[ September 30, 2008: Message edited by: bk choi ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The write lock in the code is for the recordNums hashmap. Locking for writing to the physical file is handled in the synchronized block.
 
Anthony Choi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Ericsson

Maybe my English is not good enough, hehe.

Witing to File is not what I'm worring about. When a thread tries to retrieve a record that was updated in recordNumbers but not yet written to the file, it will cause the retriveDvd() to throw IOException.

And, isn't it better for the persistDvd() to hold the writeLock until it actually finishes to write the record to the file?
 
Ericsson Liu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you only update recNums hashmap when you add a new record.

I did this in a different order. I write to the file first and then update the recNums hashmap. This way, the user can only retrieve the new record after it's written to the file.

But i guess by extending the writelock to the end of process should also work
 
Anthony Choi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ericsson, thank you for your kind reply.

I guess if i do it in different order as you said, I won't have any problem.

However, my concern still remains. Why does the code in Monkhouse's book release the writelock before it finishes writing to the file? I still wonder. any thoughts?
[ September 30, 2008: Message edited by: bk choi ]
 
Anthony Choi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind guys, I found the answer in the following thread. I cannot believe someone alreay answered to my question more than two years ago. Am I living in the past or what? haha. It turned out that the code in Monkhouse's book is not perfect. Now that I found out almost identical post exists in this forum, am I supposed to delete all my posts?

https://coderanch.com/t/188453/java-developer-SCJD/certification/Synchronization-locking-persistDVD-Andrew-book
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic