Alachisoft.NCache.Common.Stats.Clock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Alachisoft.NCache.Common.Stats;
import java.util.concurrent.atomic.AtomicLong;
public class Clock implements Runnable {
private static int _updateInterval = 1; //in seconds;
private static AtomicLong _currentTime;
private static boolean _started;
private static int _refCount;
private static Thread _timerThread;
public static void StartClock() {
synchronized (Clock.class) {
if (!_started) {
_currentTime= new AtomicLong(0);
_timerThread = new Thread(new Clock());
_timerThread.setDaemon(true);
_timerThread.start();
_started = true;
}
_refCount++;
}
}
public static void StopClock() {
synchronized (Clock.class) {
_refCount--;
if (_started && _refCount == 0) {
_started = false;
_timerThread.interrupt();
try {
_timerThread.join();
} catch (Exception e) {
}
_currentTime.set(0);
}
}
}
private static void TimerElapsed(Object sender) {
_currentTime.incrementAndGet();
}
public static long getCurrentTimeInSeconds() {
return _currentTime.get();
}
@Override
public void run() {
while (_started) {
Clock.TimerElapsed(this);
try {
Thread.sleep(_updateInterval * 1000);
} catch (InterruptedException ex) {
_started=false;
break;
} catch (Exception ex) {
}
}
}
}