01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util.concurrent;
04:
05: import com.tc.util.Assert;
06:
07: /**
08: * Some shortcut stuff for doing common thread stuff
09: *
10: * @author steve
11: */
12: public class ThreadUtil {
13:
14: public static void reallySleep(long millis) {
15: reallySleep(millis, 0);
16: }
17:
18: public static void reallySleep(long millis, int nanos) {
19: try {
20: long millisLeft = millis;
21: while (millisLeft > 0 || nanos > 0) {
22: long start = System.currentTimeMillis();
23: Thread.sleep(millisLeft, nanos);
24: millisLeft -= System.currentTimeMillis() - start;
25: nanos = 0; // Not using System.nanoTime() since it is 1.5 specific
26: }
27: } catch (InterruptedException ie) {
28: Assert.eval(false);
29: }
30: }
31:
32: /**
33: * @return <code>true</code> if the call to Thread.sleep() was successful, <code>false</code> if the call was
34: * interrupted.
35: */
36: public static boolean tryToSleep(long millis) {
37: boolean slept = false;
38: try {
39: Thread.sleep(millis);
40: slept = true;
41: } catch (InterruptedException ie) {
42: slept = false;
43: }
44: return slept;
45: }
46:
47: public static void printStackTrace(StackTraceElement ste[]) {
48: for (int i = 0; i < ste.length; i++) {
49: System.err.println("\tat " + ste[i]);
50: }
51: }
52: }
|