concurrency general strategy
synchronized(this){
Connection con = connectionMap.getByIp(ip);
if (con == null) {
con = new ConnectionFactory.createConn(..);
connectionMap.put(con);
}
try {
con.connect() // long-lasting job
} catch (Exception e) {
connectionMap.remove(con);
}
}
Is there a strategy that we can apply to remove connect() out of the synchronized block for performance consideration?
The solution is to split one big lock into n + 1 small locks. n small locks on each Connection object and one lock on the ConnectionMap. Java 5's ReentrantLock (trylock())can be used to implement non-blocking mechanism.
No comments:
Post a Comment