KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > WeakAlarm


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 import java.lang.ref.WeakReference JavaDoc;
32
33 /**
34  * The alarm class provides a lightweight event scheduler. This allows
35  * an objects to schedule a timeout without creating a new thread.
36  *
37  * <p>A separate thread periodically tests the queue for alarms ready.
38  *
39  * <p>You should use Cron for slow requests. Alarm is only
40  * appropriate for very short jobs.
41  */

42 public class WeakAlarm extends Alarm {
43   private WeakReference JavaDoc<AlarmListener> _listenerRef;
44   private WeakReference JavaDoc<ClassLoader JavaDoc> _loaderRef;
45
46     
47   /**
48    * Create a new wakeup alarm with a designated listener as a callback.
49    * The alarm is not scheduled.
50    */

51   public WeakAlarm(AlarmListener listener)
52   {
53     super(listener);
54   }
55     
56   /**
57    * Create a new wakeup alarm with a designated listener as a callback.
58    * The alarm is not scheduled.
59    */

60   public WeakAlarm(String JavaDoc name, AlarmListener listener)
61   {
62     super(name, listener);
63   }
64
65   /**
66    * Creates a named alarm and schedules its wakeup.
67    *
68    * @param name the object prepared to receive the callback
69    * @param listener the object prepared to receive the callback
70    * @param delta the time in milliseconds to wake up
71    */

72   public WeakAlarm(String JavaDoc name, AlarmListener listener, long delta)
73   {
74     super(name, listener, delta);
75   }
76   /**
77    * Creates a new alarm and schedules its wakeup.
78    *
79    * @param listener the object prepared to receive the callback
80    * @param delta the time in milliseconds to wake up
81    */

82   public WeakAlarm(AlarmListener listener, long delta)
83   {
84     this(listener);
85
86     queue(delta);
87   }
88   
89   /**
90    * Return the alarm's listener.
91    */

92   public AlarmListener getListener()
93   {
94     return _listenerRef.get();
95   }
96   
97   /**
98    * Sets the alarm's listener.
99    */

100   public void setListener(AlarmListener listener)
101   {
102     _listenerRef = new WeakReference JavaDoc<AlarmListener>(listener);
103   }
104
105   /**
106    * Sets the class loader.
107    */

108   public void setContextLoader(ClassLoader JavaDoc loader)
109   {
110     if (loader != null)
111       _loaderRef = new WeakReference JavaDoc<ClassLoader JavaDoc>(loader);
112     else
113       _loaderRef = null;
114   }
115 }
116
Popular Tags