IT/JAVA

[JAVA] TimeUnit.Sleep vs Thread.Sleep

grayhum 2015. 12. 23. 22:56

결론부터 말하자면 어차피 그놈이 그놈...




TimeUnit.Second.Sleep() 소스 까보면 다음과 같다.


     /**
     * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
     * this time unit.
     * This is a convenience method that converts time arguments into the
     * form required by the {@code Thread.sleep} method.
     *
     * @param timeout the minimum time to sleep. If less than
     * or equal to zero, do not sleep at all.
     * @throws InterruptedException if interrupted while sleeping
     */
    public void sleep(long timeout) throws InterruptedException {
        if (timeout > 0) {
            long ms = toMillis(timeout);
            int ns = excessNanos(timeout, ms);
            Thread.sleep(ms, ns);
        }
    }


TimeUnit의 static method가 직관적인게 장점!



5분 딜레이 줄때


Thread.Sleep(5 * 60 * 1000);

보다


TimeUnit.MINUTES.Sleep(5);

가 보기 편하니까.