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

io.polaris.core.clock.TimeMillisClock Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.core.clock;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Qt
 * @since 1.8
 */
public class TimeMillisClock {
	private final int period;
	private volatile long currentTimeMillis;

	private TimeMillisClock(final int period) {
		this.period = period;
		currentTimeMillis = System.currentTimeMillis();
		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, r -> new Thread(r, "Clock-" + period));
		executor.scheduleAtFixedRate(() -> currentTimeMillis = (System.currentTimeMillis()), period, period, TimeUnit.MILLISECONDS);
		Runtime.getRuntime().addShutdownHook(new Thread(() -> {
			executor.shutdown();
		}));
	}

	public static TimeMillisClock newClock(int period) {
		return new TimeMillisClock(period);
	}

	public static long currentTimeMillis() {
		return Holder.instance.currentTimeMillis;
	}

	static class Holder {
		static TimeMillisClock instance = new TimeMillisClock(1);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy