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...
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.
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.)
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).
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
Post by:autobot
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, the permaculture playing cards