Is there a reason for using random integers or the system clock inside a while loop instead of UUID.randomUUID()? You wouldn't even have to handle collisions ... "1 trillion UUIDs have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs"
John -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Andy Sent: Wednesday, April 04, 2007 12:27 PM To: Stripes Development List Cc: [EMAIL PROTECTED] Subject: [Stripes-dev] FlashScope.getCurrent (was Re: random numbers andthread synchronization) Hi Ben, On Tue, 3 Apr 2007, Ben Gunter wrote: > As a follow-up to the lengthy conversation we had about java.util.Random > and whether it is best to reuse a single one and synchronize on it or > just create a new one each time, ... ... or not using it at all... :) Which FlashScope.getCurrent code do you think will be fastest: 0. (current code) synchronized (random) { do { key = random.nextInt(); } while (scopes.containsKey(key)); scope = new FlashScope(req, key); scopes.put(scope.key(), scope); } 1. On collision use ++ instead of random.nextInt() synchronized (random) { key = random.nextInt(); while (scopes.containsKey(key)) { ++key; } scope = new FlashScope(req, key); scopes.put(scope.key(), scope); } 2. Avoid using random key = req.hashCode() + (int)System.currentTimeMillis(); synchronized (FlashScope.class) { while (scopes.containsKey(key)) { ++key; } scope = new FlashScope(req, key); scopes.put(scope.key(), scope); } 3. Avoid explicit synchronization key = req.hashCode() + (int)System.currentTimeMillis(); scope = new FlashScope(req, key); ConcurrentHashMap<Integer, FlashScope> cscopes = (ConcurrentHashMap<Integer, FlashScope>)scopes; while (cscopes.putIfAbsent(key, scope) != null) { ++key; } scope.key = key; // may be out of sync I think 1 should be faster than 0 where there's collision; not sure about 2 and 3. Andy ------------------------------------------------------------------------ - Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDE V _______________________________________________ Stripes-development mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/stripes-development ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Stripes-development mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/stripes-development
