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

85
LINES

< > BotCompany Repo | #1017897 // MultiThreadStepper

JavaX fragment (include)

sclass MultiThreadStepper implements AutoCloseable {
  Runnable onDone;
  Lock lock = lock();
  L<Thread> threads = synchroList();
  L<Steppable> steppables = synchroList();
  L<Steppable> stepping = synchroList();
  int cores = coresToUse();
  int idleDelay = 10;
  volatile bool cancelled;
  new AtomicLong steps;
  long startTime;
  volatile long elapsedTime;
  
  *() {}
  *(Steppable... steppables) { addAll(this.steppables, steppables); }
  *(Collection<? extends Steppable> steppables) { addAll(this.steppables, steppables); }
  
  void start {
    print("Using " + n2(cores, "core"));
    startTime = sysNow();
    repeat cores {
      thread "Multi Thread Stepper" {
        final Thread me = currentThread();
        threads.add(me);
        afterwards {
          bool done;
          {
            lock lock;
            threads.remove(me);
            done = empty(threads);
          }
          if (done) {
            elapsedTime = sysNow()-startTime;
            callF(onDone);
          }
        }
        //int i = 0;

        while licensed {
          Steppable s;
          {
            lock lock;
            if (done()) ret;
            //s = get(steppables, mod(i++, l(steppables)));
            s = random(steppables);
            if (s != null) {
              steppables.remove(s);
              stepping.add(s);
            }
          }
          if (s == null) continue with sleep(idleDelay);
          
          bool done = false;
          try {
            incAtomicLong(steps);
            done = !s.step();
          } catch e {
            _handleException(e);
            done = true;
          } finally {
            lock lock;
            stepping.remove(s);
            if (!done) steppables.add(s);
          }
        }
      }
    }
  }
  
  bool done() { ret empty(steppables) || cancelled; }
  
  void stop {
    cancelled = true;
    cancelThreads(threads);
  }
  
  void cleanMeUp { stop(); }
  public void close() { stop(); }
  
  MultiThreadStepper onDone(Runnable r) { onDone = r; this; }
  
  void add(Steppable s) { steppables.add(s); }
  
  long steps() { ret steps!; }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017897
Snippet name: MultiThreadStepper
Eternal ID of this version: #1017897/10
Text MD5: 9a38d3c5c0956723f28722a6e71eae8f
Author: stefan
Category: javax / execution
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-10-09 16:46:36
Source code size: 2213 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 519 / 1068
Version history: 9 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)