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

53
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 (2557L/16K).

static class ReliableSingleThread implements Runnable {
  O runnable;
  bool trigger;
  Thread thread;
  S name = "Single Thread";
  F0<AutoCloseable> enter; // optional ownership marker, e.g. for DynModules
  
  *(O *runnable) {}
  
  void trigger() { go(); }
  synchronized void go() {
    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() {
    while licensed {
      synchronized(this) {
        if (!trigger) {
          thread = null;
          break;
        }
        trigger = false;
      }
      pcallF(runnable);
    }
  }
  
  synchronized void cancel() {
    cancelAndInterruptThread(thread);
    thread = null;
  }
  
  synchronized bool triggered() { ret trigger; }
  
  void cleanMeUp { cancel(); }
}

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/13
Text MD5: 233c00ec1c00352cce537abfb0761d24
Transpilation MD5: 2581824ab10006497220478250474bb0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-28 16:44:15
Source code size: 1176 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 435 / 1576
Version history: 12 change(s)
Referenced in: [show references]

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