import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Hashtable;

/*R. Aidan Campbell
This class is an object meant to be instantiated once, and passed throughout the program
I haven't noticed any multithreaded issues, and everything still seems to function fine

The object, DNSTable, wraps around a hashmap containing the hostname as a string, and
another object, DNSEntry as the value

DNSEntry contains the expiration time, and the InetAddress of the given hostname

There may exist an issue where multiple threads access this at once and possibly cause
duplicate records.  But caching benefits dominate the duplicate query cost.
 */
public class DNSTable {

    Hashtable<String,DNSEntry> lookupTable;

    public DNSTable(){
        lookupTable = new Hashtable<String, DNSEntry>();
    }

    public DNSEntry query(String hostname){
        if(lookupTable.contains(hostname)){
            //already in table.  Check if it's expired.
            if(lookupTable.get(hostname).isExpired()){
                lookupTable.remove(hostname);//it's expired. delete the old one and put a new one
                return lookupTable.put(hostname, new DNSEntry(hostname));
            } //not expired. return the entry.
            else return lookupTable.get(hostname);
        } else{//not in table. add to table.
            return lookupTable.put(hostname, new DNSEntry(hostname));
        }
    }


    /**
     * helper object to represent the result of a DNS query.
     */
    public class DNSEntry {
        Date expirationDate;
        InetAddress address;

        public DNSEntry(String givenhostname) {
            expirationDate = new Date(System.currentTimeMillis()+30000);//expires 30 seconds after creation
            try {
                address = InetAddress.getByName(givenhostname);

            } catch (UnknownHostException e) {
                System.err.println("Error! host " + givenhostname + " not found!");
            }
        }

        /**
         * @return whether the entry is expired (30 seconds have passed)
         */
        public boolean isExpired(){
            return new Date(System.currentTimeMillis()).after(expirationDate);
        }
    }//end of DNS entry
}//end of DNS Table

@@ -100,25 +100,44 @@
 va.util.
-Hashtable
+concurrent.ConcurrentHashMap
 ;%0A%0A/*R. 
@@ -732,25 +732,41 @@
  %7B%0A%0A    
-Hashtable
+private ConcurrentHashMap
 %3CString,
@@ -842,17 +842,25 @@
 new 
-Hashtable
+ConcurrentHashMap
 %3CStr
@@ -884,31 +884,24 @@
 %0A    %7D%0A%0A    
-public 
 DNSEntry que
@@ -952,16 +952,19 @@
 contains
+Key
 (hostnam
@@ -1563,23 +1563,16 @@
  */%0A    
-public 
 class DN
@@ -1647,23 +1647,16 @@
         
-public 
 DNSEntry
@@ -2132,23 +2132,16 @@
         
-public 
 boolean 

Update DNSTable.java