一个Java获取主机名的方法
本文无技术要点,仅为备忘。
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class HostNameUtil {
private static String hostName;
public static String getHostName() throws UnknownHostException {
if (hostName != null) {
return hostName;
}
synchronized (HostNameUtil.class) {
if (hostName == null) {
InetAddress inet = InetAddress.getLocalHost();
hostName = inet.getHostName();
}
}
return hostName;
}
}