Из InetAddress.java
источник:
private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
throws UnknownHostException
{
InetAddress[] addresses = null;
boolean success = false;
UnknownHostException ex = null;
// Check whether the host is in the lookupTable.
// 1) If the host isn't in the lookupTable when
// checkLookupTable() is called, checkLookupTable()
// would add the host in the lookupTable and
// return null. So we will do the lookup.
// 2) If the host is in the lookupTable when
// checkLookupTable() is called, the current thread
// would be blocked until the host is removed
// from the lookupTable. Then this thread
// should try to look up the addressCache.
// i) if it found the addresses in the
// addressCache, checkLookupTable() would
// return the addresses.
// ii) if it didn't find the addresses in the
// 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 (host.equalsIgnoreCase("localhost")) {
InetAddress[] local = new InetAddress[] { impl.loopbackAddress() }; // {0x7f,0x00,0x00,0x01}
addresses = local;
success = true;
break;
}
Подведем итог:
- и
InetAddress.getAllByName()
, и InetAddress.getLocalHost()
разрешают адрес, вызывая getAddressesFromNameService()
- JVM поддерживает собственный кэш имени хоста -> сопоставление IP-адреса.
- Если адрес не находится в кэше (
lookupTable
или addressCache
), он вызовет DNS операционной системы (точное поведение может варьироваться в зависимости от реализации JVM).
- Отдельно для localhost - внутри
getAddressesFromNameService()