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).

1  
static class ReliableSingleThread implements Runnable {
2  
  O runnable;
3  
  S name = "Single Thread";
4  
  bool cancelBeforeTrigger; // always cancel running thread and wait for it to end before starting new operation
5  
  F0<AutoCloseable> enter; // optional ownership marker, e.g. for DynModules
6  
  int cancelTimeOut = 10000;
7  
  
8  
  bool trigger;
9  
  Thread thread;
10  
  WeakReference<Thread> threadBeingCancelled;
11  
  
12  
  *(O *runnable) {}
13  
  
14  
  void trigger() { go(); }
15  
  synchronized void go() {
16  
    if (cancelBeforeTrigger) cancel();
17  
    trigger = true;
18  
    if (!running()) {
19  
      temp callF(enter);
20  
      thread = startThread(name, r {
21  
        temp callF(enter);
22  
        _run();
23  
      });
24  
    }
25  
  }
26  
  public void run() { go(); }
27  
  void get() { go(); } // so you can use the ! syntax
28  
  
29  
  synchronized bool running() { ret thread != null; }
30  
  
31  
  // use only if this is the last time you trigger this
32  
  void triggerAndWait() {
33  
    trigger();
34  
    while (running()) sleep(1);
35  
  }
36  
  
37  
  void _run() ctex {
38  
    while licensed {
39  
      Thread oldThread;
40  
      synchronized(this) {
41  
        if (!trigger) {
42  
          thread = null;
43  
          break;
44  
        }
45  
        
46  
        oldThread = getWeakRef(threadBeingCancelled);
47  
        trigger = false;
48  
      }
49  
      
50  
      if (oldThread != null && oldThread != currentThread())
51  
        oldThread.join(cancelTimeOut);
52  
        
53  
      pcallF(runnable);
54  
    }
55  
  }
56  
  
57  
  synchronized void cancel() {
58  
    if (thread == null) ret;
59  
    threadBeingCancelled = new WeakReference(thread);
60  
    cancelAndInterruptThread(thread);
61  
    thread = null;
62  
  }
63  
  
64  
  void cancelAndTrigger() {
65  
    cancel();
66  
    trigger();
67  
  }
68  
  
69  
  synchronized bool triggered() { ret trigger; }
70  
  
71  
  void cleanMeUp { cancel(); }
72  
  
73  
  selfType cancelBeforeTrigger() {
74  
    cancelBeforeTrigger = true;
75  
    this;
76  
  }
77  
}

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: 457 / 1637
Version history: 19 change(s)
Referenced in: [show references]

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