Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max's book's code example

 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been studying the sample project code from Max's book. I have two questiones about the way of locking/unlocking.
1) In the DVDDatabase class, the method of getDVDs() retrieve the DVD info. of all the records by reading the input file. However in calling this method in DVDAdapter class, the object does not need to own the locker of all the record. Would it possible be a problem if another thread is also trying to write a record file at the same time?
2) In Max's DVD example, each record is stored in a seperated file. In my assignment, all records are in a single file. Does that mean I need an additional locker for my data file? Or could I allow multiple threads to write/read a single file at the same time, but only prohibit the write/read of same record by locking/unlocking. I am really confused.
Help please!
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
1) His findDVDs method in DVDDatabase is synchronized as all other read/write methods in this class. There is only one instance of DVDDatabase used. So executing any read/write method will block the object (nobody else can then read or write. So, it is thread-safe.
2) I didn't took attention on how Max sample works with multiple files.
In any case, since we have only one file, all methods MUST be thread-safe, however, it is up to you how you implement it.
Best,
Vlad
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I know all his methods are synchronized, but I think that's not enough, since mutliple instance may exist. That's why most of his method must obtain the locker of a record before perform anything on the record. My question is why the getDVDs() method does not need to accquire the locker?
2) In the assignment, all records are in a single file. Do I need a file locker for the entire file to avoid multi-access to the data file. If do that way, the performance will be degraded since each operation require file locker first. And more importantly, the record locker doesn't make sense anymore, since the locker has apply to the entire file, there's no need for record protection! I am really puzzled. Anybody, please help.
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
but I think that's not enough, since mutliple instance may exist.

In the sample of Max no mutliple instance may exist.

Do I need a file locker for the entire file to avoid multi-access to the data file.

No, you don't have to lock the file, since requruirement says, that only one programm may access the file.
Best,
Vlad
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
From memory, Max's book describes having a factory creating new connection objects for each client, but he did not implement this in his code. The code has one connection for every client.
Regarding your second issue, you are talking about having two threads access the database at the same time (which is allowed) whereas the specification and Vlad are talking about having two applications access the database at the same time (which is not allowed by the specification).
The only reason for locking at a file level is if two applications could access the database at the same time (so if there could be two copies of your server running, or the local client could be running at the same time as the server). However the specification specifically states that this cannot occur.
You do have to look at implementing logical locking so that two threads do not try and update the same record simultaneously, but this is not normally done at the file level.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic