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

lock() waits unlock()?

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When i review my assignment instruction, i got a problem.I show you the comments about and above lock()

// Locks a record so that it can only be updated or deleted by this client.
// Returned value is a cookie that must be used when the record is unlocked,
// updated, or deleted. If the specified record is already locked by a different
// client, the current thread gives up the CPU and consumes no CPU cycles until
// the record is unlocked.
public long lock(int recNo) throws RecordNotFoundException;


Does it mean if some other client already locked a certain record, my calling of lock() will have to wait until that client unlock? If so ,why we have to distiguish cookie and lockCookie.I mean after waiting everyclient will get the lockCookie.Can anyone explain this?
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zhixiong Pan:
Does it mean if some other client already locked a certain record, my calling of lock() will have to wait until that client unlock? If so ,why we have to distiguish cookie and lockCookie.I mean after waiting everyclient will get the lockCookie.Can anyone explain this?


Yes, your thread will have to wait.

That's correct. Your method's description states that every lock call returns a cookie that you have to use when calling the unlock method.

I couldn't understand what are these cookie and lockCookie you are talking about. By the method signature you sent, you should get one lockCookie for each record you lock and then use it later. I see no "simple cookie" being used so far.
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eiji,
I tried to start a new thread if lock() is called but have already locked by other.The new thread only wait() in run().And in unlock(), i notify() such thread.The result was nothing happened in Client, as wait() and notify() was in my Server's Data class, that is the Server waits but Client goes on.
 
Pankaja Bansal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zhixiong

I posted a similar question of what is the requirement of cookie when for all updations/ deletions we need to lock the records. But then working further and giving it a little more thought clearified my doubt.

I have created a wrapper class which will be available to client for all database enquiries. This wrapper class locks a record before updation/ deletion and then unlocks it. But imagine what will happen if the Data class is made available to client without this WRAPPER class.

The client would be free to call update/ delete method without first calling the lock method on that record. In such a case it may happen that if a record was locked by someother thread, it might still be allowed to be updated by the other client because all the Data class would check will be whether it is locked or not. Since the requirement doesn't need us to make a wrapper class, we need to make sure by the cookie, that only thread that locked that record gets to modify its contents.

I hope I made myself clear and made some some.

Cheers
PB
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zhixiong Pan:
Hi Eiji,
I tried to start a new thread if lock() is called but have already locked by other.The new thread only wait() in run().And in unlock(), i notify() such thread.The result was nothing happened in Client, as wait() and notify() was in my Server's Data class, that is the Server waits but Client goes on.


I guess it depends on how you are implementing your network (if your client waits for the server to respond to the request). But notice that it is weird to start a new empty thread just to wait. Shouldn't the original thread be the one waiting?
[ February 22, 2006: Message edited by: Eiji Seki ]
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eiji,
But notice that it is weird to start a new empty thread just to wait. Shouldn't the original thread be the one waiting?

My network is realized via RMI, so i didnot use multithread.In fact i dont know where is and what is the original thread.Should i use RMI factory insteadly?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic