KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > ApproximateClock


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: ApproximateClock.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/ApproximateClock.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:00 $
47  */

48  
49 package com.sun.enterprise.util;
50
51 /**
52  * ApproximateClock returns the approximate time instead of
53  * calling (the expensive) System.currentTimeMillis().
54  * The method getTime() returns the approximate time. The correctness
55  * of the time depends on the delay that is passed in the constructor.
56  * A background thread updates the time every 'delay' milliseconds.
57  * The exact time can still be obtained by using the getActualTime()
58  * method.
59  */

60 //Bug
61
import java.util.logging.Logger JavaDoc;
62 import java.util.logging.Level JavaDoc;
63 import com.sun.logging.LogDomains;
64 //Bug
65
public class ApproximateClock
66     implements Runnable JavaDoc
67 {
68 //Bug
69
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
70 //Bug
71
private long sleepTime = 1000;
72     private long now;
73     private Thread JavaDoc timerThread;
74     private long[] time;
75     private int index = 0;
76     private int nextIndex;
77     
78     /**
79      * Creates an ApproximateClock.
80      * @param delay The time interval to update time
81      */

82     public ApproximateClock (long delay) {
83         setDelay(delay);
84         timerThread = new Thread JavaDoc(this, "ApproximateClock");
85         timerThread.setDaemon(true);
86         time = new long[2];
87         time[0] = System.currentTimeMillis();
88         timerThread.start();
89     }
90     
91     /**
92      * Returns the actual/correct time. Same as calling System.currentTimeMillis()
93      * @return The current time (Same as System.currentTimeMillis())
94      */

95     public long getActualTime() {
96         nextIndex = 1 - index;
97         time[nextIndex] = System.currentTimeMillis();
98         index = nextIndex;
99         return time[index];
100     }
101     
102     /**
103      * Returns the approximate time. Returns the time that was updated
104      * (utmost) 'delay' milliseconds earlier.
105      * @return The approximate time
106      */

107     public long getTime() {
108         return time[index];
109     }
110     
111     /**
112      * Set the delay for updating the time.
113      * @param The delay (in milliseconds) between updates.
114      */

115     public void setDelay(long delay) {
116         sleepTime = (delay < 1) ? 1 : delay;
117     }
118     
119     /**
120      * Returns the delay
121      * @return The delay (in milliseconds) between updates.
122      */

123     public long getDelay() {
124         return sleepTime;
125     }
126     
127     /**
128      * The thread's run method to update the time periodically
129      */

130     public final void run() {
131         while (true) {
132             try {
133                 getActualTime();
134                 Thread.sleep(sleepTime);
135             } catch (InterruptedException JavaDoc inEx) {
136                 break;
137             }
138         }
139     }
140     
141     public static void main(String JavaDoc[] args)
142         throws Exception JavaDoc
143     {
144         int count = 100000000;
145         
146         long time=0, t1=0, t2 = 0;
147                 
148         t1 = System.currentTimeMillis();
149         for (int i=0; i<count; i++) {
150             time = System.currentTimeMillis();
151         }
152         t2 = System.currentTimeMillis();
153 //Bug System.out.println("sys.currentTimeMillis() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
154
_logger.log(Level.FINE,"sys.currentTimeMillis() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
155         
156         t1 = System.currentTimeMillis();
157         ApproximateClock clock = new ApproximateClock(3000);
158         for (int i=0; i<count; i++) {
159             time = clock.getTime();
160         }
161         t2 = System.currentTimeMillis();
162 //Bug System.out.println("clcok.getTime() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
163
_logger.log(Level.FINE,"clock.getTime() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
164         
165     }
166     
167 }
168
169
Popular Tags