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

44
LINES

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

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

Libraryless. Click here for Pure Java version (2947L/18K).

static class Q implements AutoCloseable {
  S name = "Unnamed Queue";
  L<Runnable> q = synchroLinkedList();
  ReliableSingleThread rst = new(r { _run() });
  volatile bool retired;
  
  *() {}
  *(S *name) {}
  
  void add(Runnable r) {
    q.add(r);
    _trigger();
  }
  
  void addInFront(Runnable r) {
    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) {
      pcall { r.run(); }
    }
  }
  
  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); }
}

download  show line numbers  debug dex   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, 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/27
Text MD5: 577215b69424db2ba3c70aea7d7b0437
Transpilation MD5: 2c5d5e5e6516b58261810a9834e92315
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 14:42:22
Source code size: 926 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 578 / 1919
Version history: 26 change(s)
Referenced in: [show references]

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