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

77
LINES

< > BotCompany Repo | #1006319 - class ReliableSingleThread - triggers again reliably when one step is already running

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3055L/19K).

static class ReliableSingleThread implements Runnable {
  O runnable;
  S name = "Single Thread";
  bool cancelBeforeTrigger; // always cancel running thread and wait for it to end before starting new operation
  F0<AutoCloseable> enter; // optional ownership marker, e.g. for DynModules
  int cancelTimeOut = 10000;
  
  bool trigger;
  Thread thread;
  WeakReference<Thread> threadBeingCancelled;
  
  *(O *runnable) {}
  
  void trigger() { go(); }
  synchronized void go() {
    if (cancelBeforeTrigger) cancel();
    trigger = true;
    if (!running()) {
      temp callF(enter);
      thread = startThread(name, r {
        temp callF(enter);
        _run();
      });
    }
  }
  public void run() { go(); }
  void get() { go(); } // so you can use the ! syntax
  
  synchronized bool running() { ret thread != null; }
  
  // use only if this is the last time you trigger this
  void triggerAndWait() {
    trigger();
    while (running()) sleep(1);
  }
  
  void _run() ctex {
    while licensed {
      Thread oldThread;
      synchronized(this) {
        if (!trigger) {
          thread = null;
          break;
        }
        
        oldThread = getWeakRef(threadBeingCancelled);
        trigger = false;
      }
      
      if (oldThread != null && oldThread != currentThread())
        oldThread.join(cancelTimeOut);
        
      pcallF(runnable);
    }
  }
  
  synchronized void cancel() {
    if (thread == null) ret;
    threadBeingCancelled = new WeakReference(thread);
    cancelAndInterruptThread(thread);
    thread = null;
  }
  
  void cancelAndTrigger() {
    cancel();
    trigger();
  }
  
  synchronized bool triggered() { ret trigger; }
  
  void cleanMeUp { cancel(); }
  
  selfType cancelBeforeTrigger() {
    cancelBeforeTrigger = true;
    this;
  }
}

Author comment

Began life as a copy of #1004705

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006319
Snippet name: class ReliableSingleThread - triggers again reliably when one step is already running
Eternal ID of this version: #1006319/20
Text MD5: 8d5b689f8df4b6ca43e4b9bc5e675d6c
Transpilation MD5: 72d1f7353de6c80c4cad5ab7cb7e4749
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-09 13:16:28
Source code size: 1869 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 458 / 1639
Version history: 19 change(s)
Referenced in: #1003674

Formerly at http://tinybrain.de/1006319 & http://1006319.tinybrain.de