Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads question!

 
xi brian
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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 lockRecord(long recNo) throws RecordNotFoundException;

// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws SecurityException.

public void unlock(long recNo, long cookie) throws SecurityException;

questions.
1.Returned value is a cookie that must be used when the record is unlocked

(does this cookie must be the recNo ?)

2.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.

(does the above mean that when current client tries to update a record which is updating by another client. the current client does not allow to do so or the current client has to wait until the lock is released.)
 
Vinicius Florentino
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1- No. It's better other, like a random, a sequencial...

2- To update the current client need to LOCK first, if other client is updating, the current will wait the lock release to get the rec lock and do something
 
xi brian
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"No. It's better other, like a random, a sequencial..."

please give me a little bit more information on this part.

i am really stuck here
 
Simon Hogg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The returned value from lock() should be the cookie that you use for unlock().

For example to update a record you need to do something like this (pseudocode):



What Vinicius is saying is that in the lock method you need to create and return a unique (long) value that you use anywhere the cookie is requested.

The cookie can either be from a sequence (e.g. the first call to lock() returns 1, the second returns 2, the third 3, etc.) or a random number and not be related to the record number at all.
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I handled the cookie.

This method is called from inside the lockRecord(long recNo) method. I declare a variable, long lockCookie = createLockCookie(recNo);
Hope this helps. There are lots of ways to deal with the cookie.
 
xi brian
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the idea
thank you very much for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic