• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max's Example in his book

 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I was going thru Max's book and could not make out why there's a FileExtension and a RecordExtension? What purpose do they serve.
1. My understanding so far is all DVDs are stored as serailized objects on the file system. How do I map the above parameters to my understanding.
2. Also, learnt that, if a client is trying to reserve a DVD and this is already in the "reservedDVDs" vector, then the client waits instead of returning. Im assuming we are not simulating the real world conditions, where when a DVD is reserved will not be available for at least five days. Is this implementation based on some understanding and trying to run home a point.
Your views will be greatly appreciated.
Vrinda.
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vrinda,
----------------------------------------------------------------
I was going thru Max's book and could not make out why there's a FileExtension and a RecordExtension? What purpose do they serve.
-----------------------------------------------------------------
If I am correct, both fileExtension and recrodExtension are same in Max's example. He is using two different varaibles to refer to the same string - ".dvd".
A file extension serves the purpose of identifying the type of file. Max's example would probably work without the extension also since 'upc' is unique for each dvd but its a good coding practice to add the extension.
-------------------------------------------------------------------------
1. My understanding so far is all DVDs are stored as serailized objects on the file system. How do I map the above parameters to my understanding.
--------------------------------------------------------------------------
I do not understand this question, so I do not know i I am answer it correctly. The file system of Max's example is equivalent to the persistent storage of the certification assignment. The assignment uses a data file with byte stream for data and Max is using the Object Stream.
------------------------------------------------------------------------
2. Also, learnt that, if a client is trying to reserve a DVD and this is already in the "reservedDVDs" vector, then the client waits instead of returning. Im assuming we are not simulating the real world conditions, where when a DVD is reserved will not be available for at least five days. Is this implementation based on some understanding and trying to run home a point.
---------------------------------------------------------------------
In Max's example a DVD is added to the 'reservedDVDs' vector only for the duration during which some user is modifying the record. As soon as someone modifies the record, the DVD is 'released' from the vector and is available for other users.
Hope this helps.
regards,
Raj
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raj.
Also, I did not observe any check on the number of copies of DVDs available and already booked. Am I right?
Correct me if I am wrong. If a thread wants to rent a DVD what is the flow in this example? What is the diff. between Renting a DVD and Reserving it?
Vrinda.
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I do not remember from the top of my head if the program is checking the number of copies. You could be right about it.
The flow of the program if two threads are trying modify the same DVD (say upc # 101) would be something like this-
i. Initially the 'reservedDVDs' vector will not have the UPC # 101 and both the threads try to reserve the DVD. Since the code is synchronized on the 'ReservedDVDs' vector, only one of them gets to execute the code of the 'reserve' method. The other thread will go to waiting stage.
ii. Assume that Thread 1 gets lock on 'ReservedDVDs'. In this case thread 2 will go to waiting stage. Thread1 executes the synchronized code and adds the upc to 'ReservedDVDs'. Once thread 1 leaves the snychronized code, the lock on 'reservedDVDs' is released. At this point upc 101 is added to 'reservedDVDs' and no thread has lock on the vector.
iii. Now one of the threads waiting for the lock on 'reservedDVDs' object will get the lock. In our example only thread2 is waiting, so it gets the lock on 'reservedDVDs' object. However, since the upc code 101 is in 'reservedDVDs', it goes back to waiting stage again.
iv. Again the system picks up one of the threads wiaitng for the lock of the vector and assigns the lock to it. As long as only thread2 is waiting for lock, step iii keeps repeating.
v. In the mean time thread1 which has reserved the upc 101, will complete the modification. It could be some operation like renting the DVD or increasing the copies by returning the DVD etc. Please note that upc 101 is still in the 'reservedDVDs' at this point.
vi. After completing the modification, thread1 calls release method. Thread1 can execute this method only after it acquires the lock on 'reservedDVDs' object. Even if thread2 presently has the lock of the object, it will immediately go back to waiting state as exolained in step iv. Therefore, thread1 will get the lock on 'reservedDVDs' in the due course and will remove the upc code 101 from the vector.
vii. Once thread1 completes the execution of rlease code, it relases the lock on the vector and thread 2 again gets the lock. This time however, since upc 101 is not in the vector, thread will complete the release method and procede with the modification.
Hope this helps....
Raj
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That definitely helped.
Thanks.
Vrinda.
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to add the following to my earlier reply.
---------------------------------------------------
Difference between renting a DVD and reserving a DVD
-----------------------------------------------------
At any point of time, many users could be trying to modify a DVD record. To ensure against the data corruption, Max has setup a protocol in his example. Any method trying to access a dvd should first reserve it and also release it as soon as they complete the modification. This is similar to acquiring the lock on the record. Only the thread which reserved the DVD will be able to modify it and other threads will continue to wait till the thread with reservation completes its modification.
cheers,
Raj
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But I did not find this being documented in the book. Correct me if I am wrong.
Vrinda
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vrinda,
I'm as clear about this as I know how to be, in chapter 4, pages 128-132, "Discussion Point: Reserving and Releasing DVDs".
All best,
M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic