KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > concurrent > locks > LockSupport_


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util.concurrent.locks;
33
34 import net.sf.retrotranslator.runtime.impl.WeakIdentityTable;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class LockSupport_ {
40
41     private static final WeakIdentityTable<Thread JavaDoc, ThreadLock> table =
42             new WeakIdentityTable<Thread JavaDoc, ThreadLock>() {
43                 protected ThreadLock initialValue() {
44                     return new ThreadLock();
45                 }
46             };
47
48     private LockSupport_() {
49     }
50
51     private static class ThreadLock {
52
53         private boolean permit;
54
55         public ThreadLock() {
56         }
57
58         public synchronized void park() throws InterruptedException JavaDoc {
59             if (permit) {
60                 permit = false;
61             } else {
62                 wait();
63             }
64         }
65
66         public synchronized void parkNanos(long nanos) throws InterruptedException JavaDoc {
67             if (permit) {
68                 permit = false;
69             } else {
70                 wait(nanos / 1000000, (int) (nanos % 1000000));
71             }
72         }
73
74         public synchronized void parkUntil(long deadline) throws InterruptedException JavaDoc {
75             if (permit) {
76                 permit = false;
77             } else {
78                 long millis = deadline - System.currentTimeMillis();
79                 if (millis > 0) {
80                     wait(millis);
81                 }
82             }
83         }
84
85         public synchronized void unpark() {
86             if (!permit) {
87                 permit = true;
88                 notify();
89             }
90         }
91
92     }
93
94     public static void park() {
95         try {
96             table.obtain(Thread.currentThread()).park();
97         } catch (InterruptedException JavaDoc e) {
98             Thread.currentThread().interrupt();
99         }
100     }
101
102     public static void parkNanos(long nanos) {
103         try {
104             table.obtain(Thread.currentThread()).parkNanos(nanos);
105         } catch (InterruptedException JavaDoc e) {
106             Thread.currentThread().interrupt();
107         }
108     }
109
110     public static void parkUntil(long deadline) {
111         try {
112             table.obtain(Thread.currentThread()).parkUntil(deadline);
113         } catch (InterruptedException JavaDoc e) {
114             Thread.currentThread().interrupt();
115         }
116     }
117
118     public static void unpark(Thread JavaDoc thread) {
119         table.obtain(thread).unpark();
120     }
121
122 }
123
Popular Tags