package uulm.teamname.marvelous.server.lobby; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; /** * A timer meant to time the entire Lifetime of the lobby, and if it is higher than the maximum permitted value, * call a callback given by the lobby to compute a winner, and quit the lobby. */ public class LifetimeTimer { private final ScheduledExecutorService timer; private final int maxGameTime; private final Runnable callback; LifetimeTimer(int maxGameTime, Runnable callback) { String lobbyThreadName = Thread.currentThread().getName(); ThreadFactory threadFactory = r -> new Thread(r, lobbyThreadName + "-LifetimeTimerThread"); this.timer = Executors.newSingleThreadScheduledExecutor(threadFactory); this.maxGameTime = maxGameTime; this.callback = callback; } void startTimer() { timer.schedule(callback, maxGameTime, TimeUnit.SECONDS); } }