• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Monkhouse book question: DvdFileAccess.persistDvd()

 
Anthony Choi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ericsson Liu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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?

http://www.coderanch.com/t/188453/java-developer-SCJD/certification/Synchronization-locking-persistDVD-Andrew-book
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic