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

70
LINES

< > BotCompany Repo | #1000934 // class Q (LIVE, runnable queue, new version without idle thread)

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

Transpiled version (7288L) is out of date.

static class Q implements AutoCloseable {
  S name = "Unnamed Queue";
  L<Runnable> q = synchroLinkedList();
  ReliableSingleThread rst = new(r { _run() });
  gettable volatile bool retired;
  gettable volatile Runnable currentJob;
  new AtomicLong jobsDone;
  
  *() {}
  *(S *name) {}
  
  void add(Runnable r) {
    assertNotRetired();
    q.add(r);
    _trigger();
  }
  
  void addInFront(Runnable r) {
    assertNotRetired();
    q.add(0, r);
    _trigger();
  }
  
  void _trigger() {
    rst.name = name;
    rst.go();
  }
  
  void add(O r) {
    add(toRunnable(r));
  }
  
  void _run() {
    Runnable r;
    while (licensed() && !retired && (r = syncPopFirst(q)) != null) {
      currentJob = r;
      inc(jobsDone);
      pcall { r.run(); }
      currentJob = null;
    }
    onIdle();
  }
  
  public void close() { retired = true; } // TODO: interrupt thread
  void done() {} // legacy function
  
  bool isEmpty() { ret q.isEmpty(); }
  int size() { ret q.size(); }
  
  O mutex() { ret q; } // clients can synchronize on this
  L<Runnable> snapshot() { ret cloneList(q); }
  
  // override this
  void onIdle {}
  
  bool busy() { ret currentJob != null; }
  
  void assertNotRetired() {
    assertFalse("Queue is retired", retired());
  }
  
  bool hasThread() { ret rst.hasThread(); }
  
  long nJobsDone() { ret jobsDone!; }
  
  toString {
    ret (retired ? "Retired " : "") + "Q " + systemHashCodeHex(this)
      + " (" + (isEmpty() ? "empty" : nEntries(size()) + ", current job: " + currentJob) + ")";
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000934
Snippet name: class Q (LIVE, runnable queue, new version without idle thread)
Eternal ID of this version: #1000934/35
Text MD5: 537eb6662c1d02cccbd0ca86c61d6862
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-03 16:32:33
Source code size: 1599 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 902 / 2822
Version history: 34 change(s)
Referenced in: [show references]