KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > thread > ShutdownLock


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.util.thread;
29
30
31 /**
32  * MobiliTools $Name: $, $Id: ShutdownLock.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
33  * <P>
34  * Sort of one-shot RW-lock. Many "reader" threads can enter (<code>in()</code>)
35  * and then quit (<code>out()</code>) the lock as long as <code>shutdown()</code>
36  * has not been invoked. Once <code>shutdown()</code> is called by a thread,
37  * other threads trying to enter the lock will get a ShutdownException.
38  * <shutdown()> invocation returns as soon as every thread has quitted the lock.
39 */

40 public class ShutdownLock
41 {
42     volatile int in = 0;
43     volatile Thread JavaDoc thread = null;
44     volatile boolean shutdown = false;
45     boolean trace = false;
46     String JavaDoc name = null;
47
48
49     /**
50         Creates a new "shutdown lock" with no name and no traces.
51     */

52     public ShutdownLock()
53     {
54     }
55
56
57     /**
58         Creates a new "shutdown lock" with the provided name, with activated traces.
59         @param lockName name that will be displayed to identify the lock in traces.
60     */

61     public ShutdownLock(String JavaDoc lockName)
62     {
63         name = lockName;
64         trace = true;
65     }
66
67
68     /**
69         Enters the lock. Don't forget to call <code>out()</code>
70         once and only once per each <code>in()</code> call.
71         @throws ShutdownException if the lock has been shutdown.
72     */

73     synchronized public void in()
74         throws ShutdownException
75     {
76         if (shutdown && Thread.currentThread() != thread)
77         {
78             throw new ShutdownException();
79         }
80         ++in;
81         if (trace)
82         {
83             System.out.println(Thread.currentThread() + " entered ShutdownLock " + name + " (=" + in + ").");
84         }
85     }
86
87
88     /**
89         Quits the lock. Must be called once and only once after each
90         <code>in()</code> invocation.
91     */

92     synchronized public void out()
93     {
94         if (--in == 0)
95         {
96             if (trace)
97             {
98                 System.out.println("ShutdownLock " + name + " is released.");
99             }
100             if (shutdown && Thread.currentThread() != thread)
101             {
102                 notify();
103             }
104         }
105         else if (trace)
106         {
107             System.out.println("ShutdownLock " + name + " = " + in + ".");
108         }
109     }
110
111
112     /**
113         Shuts the lock down. Any other thread trying to enter
114         the lock will get a ShutdownException exception.
115         Returns as soon as every other thread has quitted the lock.
116         @throws ShutdownException if the lock has already been shut down.
117     */

118     synchronized public void shutdown()
119         throws ShutdownException
120     {
121         if (shutdown)
122         {
123             throw new ShutdownException();
124         }
125         shutdown = true;
126         thread = Thread.currentThread();
127         if (in > 0)
128         {
129             if (trace)
130             {
131                 System.out.println("ShutdownLock " + name + " shutdown is pending for " + thread);
132             }
133             try
134             {
135                 wait();
136             }
137             catch (InterruptedException JavaDoc e)
138             {
139                 e.printStackTrace();
140             }
141         }
142         if (trace)
143         {
144             System.out.println("ShutdownLock " + name + " is shutdown.");
145         }
146     }
147
148
149     /**
150         Exception thrown to mean that the invoked lock has been shutdown.
151     */

152     public class ShutdownException extends Exception JavaDoc
153     {
154         public ShutdownException()
155         {
156         }
157         public ShutdownException(String JavaDoc msg)
158         {
159             super(msg);
160         }
161     }
162 }
163
Popular Tags