Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package me.j360.dubbo.modules.util.concurrent; import org.apache.commons.lang3.StringUtils; import java.util.concurrent.TimeUnit; /** * . * * 1. ?InterruptedExceptionsleep */ public class ThreadUtil { /** * sleep, ??, ???InterruptedException. */ public static void sleep(long durationMillis) { try { Thread.sleep(durationMillis); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } /** * sleep???InterruptedException. */ public static void sleep(long duration, TimeUnit unit) { try { Thread.sleep(unit.toMillis(duration)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } /** * ???InterruptedException????. */ public static void handleInterruptedException() { Thread.currentThread().interrupt(); } /** * StackTrace??. */ public static String getCallerClass() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); if (stacktrace.length >= 4) { StackTraceElement element = stacktrace[3]; return element.getClassName(); } else { return StringUtils.EMPTY; } } /** * StackTrace"??.??()" */ public static String getCallerMethod() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); if (stacktrace.length >= 4) { StackTraceElement element = stacktrace[3]; return element.getClassName() + '.' + element.getMethodName() + "()"; } else { return StringUtils.EMPTY; } } /** * StackTrace??. */ public static String getCurrentClass() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); if (stacktrace.length >= 3) { StackTraceElement element = stacktrace[2]; return element.getClassName(); } else { return StringUtils.EMPTY; } } /** * StackTrace?"??.??()" */ public static String getCurrentMethod() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); if (stacktrace.length >= 3) { StackTraceElement element = stacktrace[2]; return element.getClassName() + '.' + element.getMethodName() + "()"; } else { return StringUtils.EMPTY; } } }