KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > concurrent > ThreadUtil


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.concurrent;
5
6 import com.tc.util.Assert;
7
8 /**
9  * Some shortcut stuff for doing common thread stuff
10  *
11  * @author steve
12  */

13 public class ThreadUtil {
14
15   public static void reallySleep(long millis) {
16     reallySleep(millis, 0);
17   }
18   
19   public static void reallySleep(long millis, int nanos) {
20     try {
21       long millisLeft = millis;
22       while (millisLeft > 0 || nanos > 0) {
23         long start = System.currentTimeMillis();
24         Thread.sleep(millisLeft, nanos);
25         millisLeft -= System.currentTimeMillis() - start;
26         nanos = 0 ; // Not using System.nanoTime() since it is 1.5 specific
27
}
28     } catch (InterruptedException JavaDoc ie) {
29       Assert.eval(false);
30     }
31   }
32
33   /**
34    * @return <code>true</code> if the call to Thread.sleep() was successful, <code>false</code> if the call was
35    * interrupted.
36    */

37   public static boolean tryToSleep(long millis) {
38     boolean slept = false;
39     try {
40       Thread.sleep(millis);
41       slept = true;
42     } catch (InterruptedException JavaDoc ie) {
43       slept = false;
44     }
45     return slept;
46   }
47
48   public static void printStackTrace(StackTraceElement JavaDoc ste[]) {
49     for (int i = 0; i < ste.length; i++) {
50       System.err.println("\tat " + ste[i]);
51     }
52   }
53 }
Popular Tags