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=DEVDEV
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development