Crossfire JXClient, Trunk  R20561
EventScheduler.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.items;
23 
24 import org.jetbrains.annotations.NotNull;
25 
34 public class EventScheduler {
35 
40  private final int delay;
41 
46  private final int afterEventDelay;
47 
51  @NotNull
52  private final Runnable eventSchedulerCallback;
53 
58  @NotNull
59  private final Object sync = new Object();
60 
64  @NotNull
65  private final Thread thread;
66 
71  private long nextAction;
72 
76  private long nextActionNotBefore = System.currentTimeMillis();
77 
82  @NotNull
83  @SuppressWarnings("FieldCanBeLocal")
84  private final Runnable runnable = new Runnable() {
85 
86  @Override
87  public void run() {
88  while (true) {
89  try {
90  final long now = System.currentTimeMillis();
91  final boolean fireEvent;
92  synchronized (sync) {
93  if (nextAction == 0) {
94  sync.wait();
95  fireEvent = false;
96  } else {
97  final long thisDelay = Math.max(nextAction, nextActionNotBefore)-now;
98  if (thisDelay > 0) {
99  sync.wait(thisDelay);
100  fireEvent = false;
101  } else {
102  fireEvent = true;
103  }
104  }
105  }
106 
107  if (fireEvent) {
108  eventSchedulerCallback.run();
109  nextAction = 0;
110  nextActionNotBefore = System.currentTimeMillis()+afterEventDelay;
111  }
112  } catch (final InterruptedException ignored) {
113  thread.interrupt();
114  break;
115  }
116  }
117  }
118 
119  };
120 
127  public EventScheduler(final int delay, final int afterEventDelay, @NotNull final Runnable eventSchedulerCallback) {
128  this.delay = delay;
129  this.afterEventDelay = afterEventDelay;
130  this.eventSchedulerCallback = eventSchedulerCallback;
131  thread = new Thread(runnable, "JXClient:EventScheduler");
132  }
133 
137  public void start() {
138  thread.start();
139  }
140 
144  public void trigger() {
145  final long now = System.currentTimeMillis();
146  synchronized (sync) {
147  nextAction = now+delay;
148  sync.notifyAll();
149  }
150  }
151 
152 }
long nextActionNotBefore
The minimum timestamp for the next notification.
final int delay
The delay between a call to trigger() until the eventSchedulerCallback is notified.
final Runnable runnable
The Runnable delivering notifications through eventSchedulerCallback.
A scheduler for synchronous event notifications.
final int afterEventDelay
The minimum delay between two eventSchedulerCallback notifications.
long nextAction
The timestamp for the next notification.
final Runnable eventSchedulerCallback
The Runnable to notify.
final Thread thread
The thread running runnable.
final Object sync
The object used to synchronize access to nextAction and nextActionNotBefore.
EventScheduler(final int delay, final int afterEventDelay, @NotNull final Runnable eventSchedulerCallback)
Creates a new instance.