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

Locking and unlocking

 
Pratibha Srinivasan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
This is my first post to this great site.
I am currently implementing the FBNS assignment.
Here's how I've implemented locking. In Data class I have a HashTable with key = record number and value = the name of the thread that is trying to lock the data.
I use wait and notify on the HashTable while locking / unlocking.
To prevent a thread from unlocking a record that it has not locked, I compate the thread name with the value in the HashTable.
This approach works, but is there a risk involved in using the Thread name?
Before I end this message - could someone please tell me how to identify whether my assignment is the new one?
Thanks!
Pratibha
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12012
218
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratibha
Welcome to JavaRanch.
To be able to use the thread number within the Data class, you would have to have an instance of the Data class for each connected client. Is this correct?
If so, how are you handling one client performing an add? Each client will have their own version of Data, which will have their own version of how many records are in the database.
Any particular reason for using the old Hashtable rather than (say) a HashMap?
To use the thread name, I assume you are creating your own threads. Possibly by using sockets. Is this correct?
If you are relying on the thread currently in use by RMI, then you are in for trouble - the RMI specification is explicit in saying that there are no guarantees about what thread a call to a remote method will be run in. Two calls to a method by one client could be run in two different threads. Or calls to two different methods by two different clients could be run in the same thread (although not at the same time).
The new assignments are applications for one of:
  • A contracting company.
  • A hotel reservation system.

  • So you have the old assignment.
    Regards, Andrew
    [ July 18, 2003: Message edited by: Andrew Monkhouse ]
     
    Pratibha Srinivasan
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew,
    Thanks for the reply.
    Elaborating on the previous email:
    I am using RMI to connect to the database service.
    I have a Remote wrapper around the Data class that services all clients.
    In order to book seats, I have a single remote method bookSeats(...) within which is a sequence of lock, modify and unlock.
    I've used Hashtable since this is shared among clients.
    In the lock(int recordNo)- I check for the presence of the record number in the Hashtable and call wait or lock the record number as appropriate. The key in the hash is the record number and the value is the thread name
    In the unlock(int recordNo) - I check for the presence of the record number in the hash and get the value. If the value=name of the current thread then I remove the record number from the hash.
    Would there be any problems with this considering that the record is unlocked within the same method call?
    Regards,
    Pratibha
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12012
    218
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Pratibha
    OK, If you are using RMI, then you should not be using the thread as your client identifier, as you have no gurarantees what thread will be used.
    From the RMI Specifications:
    A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads.

    In other words, the following is possible:
  • client A calls method1 --> executes in thread 1
  • client A calls method2 --> executes in thread 2
  • client B calls method1 --> executes in thread 2
  • client B calls method2 --> executes in thread 5


  • Data.lock() only takes an integer as a parameter. You cannot pass the thread name as a parameter. So how are you getting the thread name in to your Hashtable? Do you have multiple instances of the Data class?
    I think you are saying that your method bookSeats() is running on the server. Is this correct?
    So this means that a remote client (not one you wrote) could call bookSeats(). Alternatively it could call lock(), read(), modify(), and unlock(). Is this correct?
    I think I had better stop posting. I might scare you away with all my questions
    Regards, Andrew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic