• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Max's book's code example

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic