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)

1  
static class Q implements AutoCloseable {
2  
  S name = "Unnamed Queue";
3  
  BlockingQueue<Runnable> q = new LinkedBlockingQueue;
4  
  volatile bool retired;
5  
  
6  
  static class Done extends RuntimeException {}
7  
  
8  
  *() {}
9  
  
10  
  *(bool startThread) {
11  
    if (startThread) startThread();
12  
  }
13  
  
14  
  *(String *name, boolean startThread) {
15  
    if (startThread) startThread();
16  
  }
17  
  
18  
  Runnable _mainRunnable() {
19  
    ret r { Q.this.run(); };
20  
  }
21  
  
22  
  Thread startThread() {
23  
    ret _startThread(name, _mainRunnable());
24  
  }
25  
  
26  
  Thread startDaemonThread() {
27  
    ret _startThread(newDaemonThread(name, _mainRunnable()));
28  
  }
29  
  
30  
  Iterable<Runnable> master() {
31  
    return new Iterable<Runnable>() {
32  
      public Iterator<Runnable> iterator() {
33  
        return new Iterator<Runnable>() {
34  
          Runnable x;
35  
          
36  
          public boolean hasNext() ctex {
37  
            //debug("hasNext");
38  
            while (x == null && licensed() && !retired)
39  
              x = q.poll(1, TimeUnit.SECONDS);
40  
            //debug("hasNext true");
41  
            return true;
42  
          }
43  
          
44  
          public Runnable next() {
45  
            if (!licensed() || retired) null;
46  
            //debug("next");
47  
            hasNext();
48  
            Runnable _x = x;
49  
            x = null;
50  
            //debug("next " + structure(x));
51  
            return _x;
52  
          }
53  
          
54  
          public void remove() {
55  
          }
56  
        };
57  
      }
58  
    };
59  
  }
60  
  
61  
  void add(Runnable r) {
62  
    q.add(r);
63  
  }
64  
  
65  
  void add(O r) {
66  
    q.add(toRunnable(r));
67  
  }
68  
  
69  
  void run() {
70  
    for (Runnable r : master()) {
71  
      if (!licensed() || retired) ret;
72  
      try {
73  
        r.run();
74  
      } catch (Done e) {
75  
        ret; // break signal
76  
      } catch (Throwable e) {
77  
        printStackTrace(e);
78  
      }
79  
    }
80  
  }
81  
  
82  
  // end after current items have been processed
83  
  void done() {
84  
    add(r { throw new Done; });
85  
  }
86  
  
87  
  public void close() { retired = true; } // TODO: interrupt thread
88  
  
89  
  bool isEmpty() { ret q.isEmpty(); }
90  
}

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: 432 / 457
Referenced in: [show references]