Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not entirely sure if you need it to work like this, but the logic behind it is that when eventB is fired, it adds the data to a ConcurrentHashMap, and schedules itself to be removed after 3 seconds. When eventA is fired, it removes the data from the map (thus clearing the event for that key), and if there was an event, it calls doSomething(int, long), otherwise it calls doSomething(int).</p> <pre><code>import java.util.concurrent.*; public class EventListenerImpl implements EventListener { // You can change how many threads there are, or use a scheduler that's // already in the application. private final static ScheduledExecutorService scheduler = Executors .newScheduledThreadPool(1); private ConcurrentHashMap&lt;Integer, Data&gt; data = new ConcurrentHashMap&lt;Integer, Data&gt;(); @Override public void eventA(int a) { Data b = data.remove(a); if (b != null) { doSomething(a, b.getB()); } else { doSomething(a); } } @Override public void eventB(final int a, final long b) { final Data thisData = new Data(a); // Needs to be synchronized because it may be removed by a scheduled event synchronized (data) { Data d = data.put(a, thisData); if (d != null) { d.getFuture().cancel(true); } } thisData.setFuture(scheduler.schedule(new Runnable() { @Override public void run() { data.remove(a, thisData); } }, 3, TimeUnit.SECONDS)); } private class Data { private long b; private ScheduledFuture&lt;?&gt; future; public Data(long b) { this.b = b; } public long getB() { return b; } public void setFuture(ScheduledFuture&lt;?&gt; future) { this.future = future; } public ScheduledFuture&lt;?&gt; getFuture() { return future; } } } </code></pre> <p>Let me know if this isn't quite what you need, because I was a bit confused, and ended up guessing what you were trying to do. For example, if <code>a</code> isn't a key of some sort, then this can be greatly simplified... It can also be simplified if eventA waits three seconds before calling doSomething, rather than tracking the last three seconds at all times.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload