KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > ThreadUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 /**
6  * Thread utilities.
7  */

8
9 public class ThreadUtil {
10
11     /**
12      * Puts a thread to sleep, without throwing an InterruptedException.
13      *
14      * @param ms the length of time to sleep in milliseconds
15      */

16     public static void sleep(long ms) {
17         try {
18             Thread.sleep(ms);
19         } catch (InterruptedException JavaDoc iex) {
20             // ignore
21
}
22     }
23
24
25     /**
26      * Puts a thread to sleep forever.
27      */

28     public static void sleep() {
29         try {
30             Thread.sleep(Long.MAX_VALUE);
31         } catch (InterruptedException JavaDoc iex) {
32             // ignore
33
}
34     }
35
36
37     // ---------------------------------------------------------------- synchronization
38

39     /**
40      * Waits for a object for synchronization purposes.
41      */

42     public static void wait(Object JavaDoc obj) {
43         synchronized (obj) {
44             try {
45                 obj.wait();
46             } catch (InterruptedException JavaDoc inex) {
47                 //ignore
48
}
49         }
50     }
51
52     /**
53      * Waits for a object or a timeout for synchronization purposes.
54      */

55     public static void wait(Object JavaDoc obj, long timeout) {
56         synchronized (obj) {
57             try {
58                 obj.wait(timeout);
59             } catch (InterruptedException JavaDoc inex) {
60                 // ignore
61
}
62         }
63     }
64
65     /**
66      * Notifies an object for synchronization purposes.
67      */

68     public static void notify(Object JavaDoc obj){
69         synchronized (obj) {
70             obj.notify();
71         }
72     }
73
74     /**
75      * Notifies an object for synchronization purposes.
76      */

77     public static void notifyAll(Object JavaDoc obj){
78         synchronized (obj) {
79             obj.notifyAll();
80         }
81     }
82
83 }
Popular Tags