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: 839 / 2746
Version history: 34 change(s)
Referenced in: #1000933 - Thread bridge example
#1003872 - Integrating #759 in One Program
#1003874 - Backup of #759 Before Integration
#1004091 - 759 with new loadClasses (spike)
#1015387 - class DeQ (runnable queue with ability to insert tasks at the front)
#1017607 - class Q2 (now promoted to "Q", runnable queue that drops the thread when not needed)
#1017610 - class Q (runnable queue, old version)
#1024003 - QWithObjects (runnable queue with arbitrary job objects, dev.)
#1032857 - CompactQ - Q (thread-based queue) but in compact - only 32 bytes when idle (including Java header!)
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)