Again, something to weigh. If I do this:
private static final String getLocalName(HttpServletRequest request) {
| String localAddr = request.getLocalAddr();
| String localName = localAddr_to_localName.get(localAddr);
| if (localName == null) {
| localName = request.getLocalName();
| localAddr_to_localName.put(localAddr, localName);
| }
| return localName;
| }
| private static final Map<String,String> localAddr_to_localName =
Collections.synchronizedMap(new HashMap<String,String>());
The it is thread safe, but opens the door for multiple calls to request.getLocalName().
But if I do this:
private static final String getLocalName(HttpServletRequest request) {
| String localAddr = request.getLocalAddr();
| synchronized (localAddr_to_localName) {
| String localName = localAddr_to_localName.get(localAddr);
| if (localName == null) {
| localName = request.getLocalName();
| localAddr_to_localName.put(localAddr, localName);
| }
| return localName;
| }
| }
| private static final Map<String,String> localAddr_to_localName = new
HashMap<String,String>();
Then request.getLocalName() will only ever get called once, since both the get and put
calls to the map are in the same atomic block.
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4269383#...
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&a...