-
Notifications
You must be signed in to change notification settings - Fork 6
Utils
Thomas Schwotzer edited this page Aug 16, 2021
·
11 revisions
ASAP became a fairly huge project after a while. Some useful little classes are implemented over some time. Some could be useful in your application. Feel free to use them. Why implementing such little things over and over again.
Distributed systems are often need timeouts. A message is sent to another peer. We expect some result within a given time frame. What would we do in real life? Go to bed and set the alarm. Meet class AlarmClock.
class YourClass implements AlarmClockListener {
...
// define constants
public static final int TASK_0 = 0;
public static final int TASK_1 = 1;
// implement AlarmClockListener - what happens after alarm
public void alarmClockRinging(int yourKey) {
switch (yourKey) {
case TASK_0: /* do something */; break;
case TASK_1: /* do something else */; break;
}
}
...
long timeout0 = 1000;
// set alarm - your class will be called in 1000 milliseconds
new AlarmClock(timeout0, TASK_0, this).start();
// another alarm - 5 seconds
new AlarmClock(5000, TASK_0, this).start();