All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Stats.Clock Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * 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) {
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy