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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM