欢迎访问我的个人网站==》 jiashubing.cn

Java 获取当前线程、进程、服务器ip

/**
 * 获取当前线程id
 */
private Long getThreadId() {
    try {
        return Thread.currentThread().getId();
    } catch (Exception e) {
        return null;
    }
}

/**
 * 获取当前进程id
 */
private Long getProcessId() {
    try {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String name = runtime.getName();
        String pid = name.substring(0, name.indexOf('@' ));
        return Long.parseLong(pid);
    } catch (Exception e) {
        return null;
    }
}

/**
 * 获取当前服务器ip地址
 */
private String getServerIp() {
    try {
        //用 getLocalHost() 方法创建的InetAddress的对象
        InetAddress address = InetAddress.getLocalHost();
        return address.getHostAddress();
    } catch (Exception e) {
        return null;
    }
}

 

posted @ 2019-04-28 15:57 贾树丙 阅读(...) 评论(...) 编辑 收藏