src/share/classes/java/net/InetAddress.java

Print this page

        

@@ -675,12 +675,11 @@
 
     static InetAddress[]    unknown_array; // put THIS in cache
 
     static InetAddressImpl  impl;
 
-    private static HashMap<String, InetAddress[]> lookupTable
-        = new HashMap<String, InetAddress[]>();
+    private static final HashMap<String, Void> lookupTable = new HashMap<>();
 
     /**
      * Represents a cache entry
      */
     static final class CacheEntry {

@@ -735,11 +734,11 @@
 
             if (policy != InetAddressCachePolicy.FOREVER) {
 
                 // As we iterate in insertion order we can
                 // terminate when a non-expired entry is found.
-                LinkedList<String> expired = new LinkedList<String>();
+                LinkedList<String> expired = new LinkedList<>();
                 long now = System.currentTimeMillis();
                 for (String key : cache.keySet()) {
                     CacheEntry entry = cache.get(key);
 
                     if (entry.expiration >= 0 && entry.expiration < now) {

@@ -1225,10 +1224,11 @@
         //         addressCache for any reason,
         //         it should add the host in the
         //         lookupTable and return null so the
         //         following code would do  a lookup itself.
         if ((addresses = checkLookupTable(host)) == null) {
+            try {
             // This is the first thread which looks up the addresses
             // this host or the cache entry for this host has been
             // expired so this thread should do the lookup.
             for (NameService nameService : nameServices) {
                 try {

@@ -1256,33 +1256,31 @@
                 }
             }
 
             // Cache the addresses.
             cacheAddresses(host, addresses, success);
-            // Delete the host from the lookupTable, and
-            // notify all threads waiting for the monitor
-            // for lookupTable.
-            updateLookupTable(host);
             if (!success && ex != null)
                 throw ex;
+            } finally {
+                // Delete host from the lookupTable and notify
+                // all threads waiting on the lookupTable monitor.
+                updateLookupTable(host);
         }
+        }
 
         return addresses;
     }
 
 
     private static InetAddress[] checkLookupTable(String host) {
-        // make sure addresses is null.
-        InetAddress[] addresses = null;
-
         synchronized (lookupTable) {
             // If the host isn't in the lookupTable, add it in the
             // lookuptable and return null. The caller should do
             // the lookup.
             if (lookupTable.containsKey(host) == false) {
                 lookupTable.put(host, null);
-                return addresses;
+                return null;
             }
 
             // If the host is in the lookupTable, it means that another
             // thread is trying to look up the addresses of this host.
             // This thread should wait.

@@ -1296,14 +1294,15 @@
 
         // The other thread has finished looking up the addresses of
         // the host. This thread should retry to get the addresses
         // from the addressCache. If it doesn't get the addresses from
         // the cache, it will try to look up the addresses itself.
-        addresses = getCachedAddresses(host);
+        InetAddress[] addresses = getCachedAddresses(host);
         if (addresses == null) {
             synchronized (lookupTable) {
                 lookupTable.put(host, null);
+                return null;
             }
         }
 
         return addresses;
     }