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

37
LINES

< > BotCompany Repo | #1024003 // QWithObjects (runnable queue with arbitrary job objects, dev.)

JavaX fragment (include)

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

Author comment

Began life as a copy of #1000934

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024003
Snippet name: QWithObjects (runnable queue with arbitrary job objects, dev.)
Eternal ID of this version: #1024003/1
Text MD5: 7bc81519593658c276ea142e9ea5449f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-15 15:40:52
Source code size: 762 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 123 / 148
Referenced in: [show references]