• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S Is it OK To Generate The long Value For The Lock in "lockRecord()" Randomly?

 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 151 of Andrews book, he hints at the possibility of generating the locks randomly, but I can't find where it definitely says this is acceptable or not. How are the rest of you determining what to return when the lockRecord() method is used? Thanks so much for any hints...
 
Bob Nedwor
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like I just found it. This topic seems to mean that this is pretty much the way to do it:

topic
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this approach. I would bet money against the possibility of a call to Random.NextLog returning duplicate locks..its possible but extremely improbable.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I intend on using it, but just in case I will kind of keep track of used longs so I guarantee there will be no duplicates.
 
Bob Nedwor
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Eiji. Excellent point.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you use Random, then keep track of what has been used... this really sounds like a lot of work. Couldn't you have a static int in your LockManager class which incremented in the LockRecord() method, and handed back in a thread-safe way? (I didn't use locking cookies in my assignment, so I could be way off base here.)

Lara
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you could do that also.

The random solution, on the other hand, is more complete, since it is safer. It is a real life solution, so it is not mandatory (the increment is enough), but since it is not that difficult to do, I see no problem.

Just imagine if your web server used an incremental session ID instead of a random one. If you just saw your session ID is 1234, you could probably use 1233 and get a valid session, luckly with credit card information or other confidential information already inputed.

Also if you want to increase performance, the random solution allows you to generate id's without synchronization. Off course, having the possiblity of collision (larger or smalled depending on keeping tracks or not).
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lara McCarver:
Why would you use Random, then keep track of what has been used... this really sounds like a lot of work. Couldn't you have a static int in your LockManager class which incremented in the LockRecord() method, and handed back in a thread-safe way? (I didn't use locking cookies in my assignment, so I could be way off base here.)

Lara



Hi Lara
Here's a solution of generating it randomly using the full long range without lot of work

Long value;
Map lockedRecs;
do {
long lRandom = (long)((Math.random() * 2 -1) * Long.MAX_VALUE);
value = new Long(lRandom);
}
while(lockedRecs.containsValue(value));

Yupp
 
reply
    Bookmark Topic Watch Topic
  • New Topic