001 /*
002 * $Id: ThreadUtil.java,v 1.6 2012/01/16 21:58:11 oboehm Exp $
003 *
004 * Copyright (c) 2008 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 30.09.2008 by oliver (ob@oasd.de)
019 */
020 package patterntesting.runtime.util;
021
022 import java.util.concurrent.TimeUnit;
023
024 import org.slf4j.*;
025
026 /**
027 * The Class ThreadUtil.
028 *
029 * @author <a href="boehm@javatux.de">oliver</a>
030 * @since 30.09.2008
031 * @version $Revision: 1.6 $
032 */
033 public final class ThreadUtil {
034
035 private static Logger log = LoggerFactory.getLogger(ThreadUtil.class);
036 private static final long timeInMillis;
037
038 static {
039 timeInMillis = calibrateMillis();
040 log.debug("timeInMillis calibrated (" + timeInMillis + "ms)");
041 }
042
043 /** to avoid instantiation it's now private */
044 private ThreadUtil() {}
045
046 /**
047 * Sleep.
048 */
049 public static void sleep() {
050 sleep(timeInMillis);
051 }
052
053 /**
054 * Sleep.
055 *
056 * @param millis the millis
057 */
058 public static void sleep(final long millis) {
059 try {
060 Thread.sleep(millis);
061 } catch (InterruptedException ignored) {
062 log.info(ignored + " ignored");
063 }
064 }
065
066 /**
067 * Sleep.
068 *
069 * @param millis the millis
070 * @param nanos the nanos
071 */
072 public static void sleep(final long millis, final int nanos) {
073 try {
074 Thread.sleep(millis, nanos);
075 } catch (InterruptedException ignored) {
076 log.info(ignored + " ignored");
077 }
078 }
079
080 /**
081 * Using now also the constants MINUTES, HOURS and DAYS which are new
082 * with Java 6.
083 *
084 * @param time e.g. 5
085 * @param unit e.c. SECONDS
086 */
087 public static void sleep(final int time, final TimeUnit unit) {
088 switch (unit) {
089 case NANOSECONDS:
090 sleep(0L, time);
091 break;
092 case MICROSECONDS:
093 sleep(0L, time * 1000);
094 break;
095 case MILLISECONDS:
096 sleep(time);
097 break;
098 case SECONDS:
099 sleep(time * 1000L);
100 break;
101 // case MINUTES:
102 // sleep(time * 1000L * 60);
103 // break;
104 // case HOURS:
105 // sleep(time * 1000L * 60 * 60);
106 // break;
107 // case DAYS:
108 // sleep(time * 1000L * 60 * 60 * 24);
109 // break;
110 default:
111 log.warn("unknown TimeUnit " + unit + " interpreting it as WEEKS");
112 sleep(time * 1000L * 60 * 60 * 24 * 7);
113 break;
114 }
115 }
116
117 /**
118 * Determines the timer resolution for the sleep() method.
119 * On windows this is 15 ms, on Linux and MacOS 1 ms.
120 *
121 * @return 1 (ms) on Linux and MacOS, 15 (ms) on Windows
122 */
123 private static long calibrateMillis() {
124 long t0 = nextTimeMillis();
125 long t1 = nextTimeMillis();
126 return t1 - t0;
127 }
128
129 /**
130 * Go to the next timer tick.
131 * @return the next timestamp
132 */
133 private static long nextTimeMillis() {
134 long t0 = System.currentTimeMillis();
135 while (t0 == System.currentTimeMillis()) { // SUPPRESS CHECKSTYLE
136 // do nothing
137 }
138 return System.currentTimeMillis();
139 }
140
141 /**
142 * Gets the timer resolution in millis.
143 *
144 * @return the timer resolution in millis
145 */
146 public static long getTimerResolutionInMillis() {
147 return timeInMillis;
148 }
149
150 }