Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

97
LINES

< > BotCompany Repo | #1033541 // ReliableExecuteAfterChange [dev.] - replacement for ReliableSingleThread/RSTOverQ with new execution constructs [dev.]

JavaX fragment (include)

// run = trigger (for convenience)
sclass ReliableExecuteAfterChange is Runnable, AutoCloseable {
  Runnable action;
  IActionScheduler actionScheduler;
  bool trigger;
  bool running;
  ScheduledAction thread;
  
  *(Runnable *action, IActionScheduler *actionScheduler) {}
  *(IActionScheduler *actionScheduler, Runnable *action) {}
  
  bool cancelBeforeTrigger; // always cancel running thread and wait for it to end before starting new operation
  bool waitBetweenCancelAndTrigger; // make sure the old thread is actually ended
  bool cancelOnClose;
  int cancelTimeOut = 10000;
  
  synchronized public void trigger aka run aka get() {
    if (trigger)
    if (cancelBeforeTrigger) cancelAndPossiblyWait();
    trigger = true;
    shouldRun = !running())
      starting
      thread = actionScheduler.start(r _run);
  }
  
  synchronized bool running() { ret thread != null; }
  
  // use only if this is the last time you trigger this
  void triggerAndWait() {
    trigger();
    waitUntilDone();
  }
  
  void waitUntilDone {
    while (running()) sleep(1);
  }
  
  private void _run() ctex {
    while true {
      synchronized(this) {
        if (!trigger)
          ret;
        running = true;
        trigger = false;
      }
  
      try {
        action?!;
      } finally {
        synchronized(this) {
          running = false;
        }
      }
  }
  
  close {
    if (cancelOnClose)
      cancel();
  }
  
  void cancel() {
    ScheduledAction toCancel;
    synchronized { toCancel = thread; }
    if (toCancel == null) ret;
    cancelAndInterruptScheduledAction(toCancel);
  }
  
  void cancelAndWait() {
    Thread _thread;
    
    synchronized {
      if (thread == null) ret;
      _thread = thread;
      threadBeingCancelled = new WeakReference(thread);
      thread = null;
    }
    
    cancelAndInterruptThread(_thread);
  }
  
  void cancelAndTrigger() {
    cancelAndPossiblyWait();
    trigger();
  }
  
  synchronized bool triggered() { ret trigger; }
  
  selfType cancelBeforeTrigger() {
    cancelBeforeTrigger = true;
    this;
  }
  
  void cancelAndPossiblyWait() {
    if (waitBetweenCancelAndTrigger)
    cancel();
  }
}

Author comment

Began life as a copy of #1006319

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033541
Snippet name: ReliableExecuteAfterChange [dev.] - replacement for ReliableSingleThread/RSTOverQ with new execution constructs [dev.]
Eternal ID of this version: #1033541/4
Text MD5: ff69aa55c6978baa2abab6dea33a68d6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-01 23:49:10
Source code size: 2260 bytes / 97 lines
Pitched / IR pitched: No / No
Views / Downloads: 71 / 89
Version history: 3 change(s)
Referenced in: [show references]