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

ru.shemplo.snowball.utils.time.TimeoutEvent Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package ru.shemplo.snowball.utils.time;

import java.util.concurrent.atomic.AtomicLong;

public class TimeoutEvent {
	
	private final AtomicLong LAST_UPDATED = new AtomicLong ();
	private final Runnable EVENT;
	private final long TIMEOUT;
	
	private long timer = 0;
	
	public TimeoutEvent (long timeout, Runnable event) {
		this.LAST_UPDATED.set (System.currentTimeMillis ());
		this.TIMEOUT = timeout;
		this.EVENT = event;
	}
	
	public void update (long delta) {
		long current = System.currentTimeMillis (), 
			 value = LAST_UPDATED.get ();
		
		if (LAST_UPDATED.compareAndSet (value, current)) {
			timer += Math.min (delta + 1, current - value);
			if (timer > TIMEOUT) {
				EVENT.run ();
				timer = 0;
			}
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy