@Override public void run() { while (start) { int currentSlotTemp = currentSlot; ConcurrentLinkedQueue<TimerTask> timerSlot = timerSlots[currentSlotTemp++]; currentSlotTemp %= timerSlots.length; for (Iterator<TimerTask> iterator = timerSlot.iterator(); iterator .hasNext();) { if (iterator.next().runTask()) iterator.remove(); } try { Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } currentSlot = currentSlotTemp; } }
/** * add a task * * @param delay * after x milliseconds than execute runnable * @param run * run task in future * @return The task future */ public Future add(long delay, Runnable run) { final int curSlot = currentSlot; final int ticks = delay > interval ? (int) (delay / interval) : 1; // figure out how many ticks need final int index = (curSlot + (ticks % maxTimers)) % maxTimers; // figure out the wheel's index final int round = (ticks - 1) / maxTimers; // the round number of spin TimerTask task = new TimerTask(round, run); timerSlots[index].add(task); return new Future(this, index, task); }
/** * add a task * * @param delay after x milliseconds than execute runnable * @param run run task in future * @return The task future */ public Future add(long delay, Runnable run) { final int curSlot = currentSlot; final int ticks = delay > interval ? (int) (delay / interval) : 1; // figure out how many ticks need final int index = (curSlot + (ticks % maxTimers)) % maxTimers; // figure out the wheel's index final int round = (ticks - 1) / maxTimers; // the round number of spin TimerTask task = new TimerTask(round, run); timerSlots[index].add(task); return new Future(this, index, task); }
/** * add a task * * @param delay after x milliseconds than execute runnable * @param run run task in future * @return The task future */ public Future add(long delay, Runnable run) { final int curSlot = currentSlot; final int ticks = delay > interval ? (int) (delay / interval) : 1; // figure out how many ticks need final int index = (curSlot + (ticks % maxTimers)) % maxTimers; // figure out the wheel's index final int round = (ticks - 1) / maxTimers; // the round number of spin TimerTask task = new TimerTask(round, run); timerSlots[index].add(task); return new Future(this, index, task); }