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

90
LINES

< > BotCompany Repo | #1017610 // class Q (runnable queue, old version)

JavaX fragment (include)

static class Q implements AutoCloseable {
  S name = "Unnamed Queue";
  BlockingQueue<Runnable> q = new LinkedBlockingQueue;
  volatile bool retired;
  
  static class Done extends RuntimeException {}
  
  *() {}
  
  *(bool startThread) {
    if (startThread) startThread();
  }
  
  *(String *name, boolean startThread) {
    if (startThread) startThread();
  }
  
  Runnable _mainRunnable() {
    ret r { Q.this.run(); };
  }
  
  Thread startThread() {
    ret _startThread(name, _mainRunnable());
  }
  
  Thread startDaemonThread() {
    ret _startThread(newDaemonThread(name, _mainRunnable()));
  }
  
  Iterable<Runnable> master() {
    return new Iterable<Runnable>() {
      public Iterator<Runnable> iterator() {
        return new Iterator<Runnable>() {
          Runnable x;
          
          public boolean hasNext() ctex {
            //debug("hasNext");
            while (x == null && licensed() && !retired)
              x = q.poll(1, TimeUnit.SECONDS);
            //debug("hasNext true");
            return true;
          }
          
          public Runnable next() {
            if (!licensed() || retired) null;
            //debug("next");
            hasNext();
            Runnable _x = x;
            x = null;
            //debug("next " + structure(x));
            return _x;
          }
          
          public void remove() {
          }
        };
      }
    };
  }
  
  void add(Runnable r) {
    q.add(r);
  }
  
  void add(O r) {
    q.add(toRunnable(r));
  }
  
  void run() {
    for (Runnable r : master()) {
      if (!licensed() || retired) ret;
      try {
        r.run();
      } catch (Done e) {
        ret; // break signal
      } catch (Throwable e) {
        printStackTrace(e);
      }
    }
  }
  
  // end after current items have been processed
  void done() {
    add(r { throw new Done; });
  }
  
  public void close() { retired = true; } // TODO: interrupt thread
  
  bool isEmpty() { ret q.isEmpty(); }
}

Author comment

Began life as a copy of #1000934

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017610
Snippet name: class Q (runnable queue, old version)
Eternal ID of this version: #1017610/1
Text MD5: cf076c27dc2228ecd10666410cfaf2b3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-29 18:37:13
Source code size: 2064 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 431 / 455
Referenced in: [show references]