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)

1  
// run = trigger (for convenience)
2  
sclass ReliableExecuteAfterChange is Runnable, AutoCloseable {
3  
  Runnable action;
4  
  IActionScheduler actionScheduler;
5  
  bool trigger;
6  
  bool running;
7  
  ScheduledAction thread;
8  
  
9  
  *(Runnable *action, IActionScheduler *actionScheduler) {}
10  
  *(IActionScheduler *actionScheduler, Runnable *action) {}
11  
  
12  
  bool cancelBeforeTrigger; // always cancel running thread and wait for it to end before starting new operation
13  
  bool waitBetweenCancelAndTrigger; // make sure the old thread is actually ended
14  
  bool cancelOnClose;
15  
  int cancelTimeOut = 10000;
16  
  
17  
  synchronized public void trigger aka run aka get() {
18  
    if (trigger)
19  
    if (cancelBeforeTrigger) cancelAndPossiblyWait();
20  
    trigger = true;
21  
    shouldRun = !running())
22  
      starting
23  
      thread = actionScheduler.start(r _run);
24  
  }
25  
  
26  
  synchronized bool running() { ret thread != null; }
27  
  
28  
  // use only if this is the last time you trigger this
29  
  void triggerAndWait() {
30  
    trigger();
31  
    waitUntilDone();
32  
  }
33  
  
34  
  void waitUntilDone {
35  
    while (running()) sleep(1);
36  
  }
37  
  
38  
  private void _run() ctex {
39  
    while true {
40  
      synchronized(this) {
41  
        if (!trigger)
42  
          ret;
43  
        running = true;
44  
        trigger = false;
45  
      }
46  
  
47  
      try {
48  
        action?!;
49  
      } finally {
50  
        synchronized(this) {
51  
          running = false;
52  
        }
53  
      }
54  
  }
55  
  
56  
  close {
57  
    if (cancelOnClose)
58  
      cancel();
59  
  }
60  
  
61  
  void cancel() {
62  
    ScheduledAction toCancel;
63  
    synchronized { toCancel = thread; }
64  
    if (toCancel == null) ret;
65  
    cancelAndInterruptScheduledAction(toCancel);
66  
  }
67  
  
68  
  void cancelAndWait() {
69  
    Thread _thread;
70  
    
71  
    synchronized {
72  
      if (thread == null) ret;
73  
      _thread = thread;
74  
      threadBeingCancelled = new WeakReference(thread);
75  
      thread = null;
76  
    }
77  
    
78  
    cancelAndInterruptThread(_thread);
79  
  }
80  
  
81  
  void cancelAndTrigger() {
82  
    cancelAndPossiblyWait();
83  
    trigger();
84  
  }
85  
  
86  
  synchronized bool triggered() { ret trigger; }
87  
  
88  
  selfType cancelBeforeTrigger() {
89  
    cancelBeforeTrigger = true;
90  
    this;
91  
  }
92  
  
93  
  void cancelAndPossiblyWait() {
94  
    if (waitBetweenCancelAndTrigger)
95  
    cancel();
96  
  }
97  
}

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: 82 / 100
Version history: 3 change(s)
Referenced in: [show references]