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

37
LINES

< > BotCompany Repo | #1035343 // RunnablesReferenceQueue - a reference queue for Reference objects that implement Runnable (will be run when enqueued)

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

Libraryless. Click here for Pure Java version (9358L/52K).

1  
// Note: Costs one thread while it exists
2  
sclass RunnablesReferenceQueue is AutoCloseable {
3  
  new ReferenceQueue queue;
4  
  volatile Thread thread;
5  
  new Flag closed;
6  
  
7  
  settable int timeout = 60000; // just for safety
8  
  
9  
  *() {
10  
    thread = startThread("RunnablesReferenceQueue", l0 _run);
11  
  }
12  
  
13  
  void _run() ctex {
14  
    try {
15  
      while (ping() && !closed!) {
16  
        Reference<?> ref = queue.remove(timeout);
17  
        if (ref != null) {
18  
          if (ref cast Runnable)
19  
            pcallF(ref);
20  
          else
21  
            warn("RunnablesReferenceQueue: Reference not runnable - "
22  
              + className(ref));
23  
        }
24  
      }
25  
    } catch InterruptedException e {
26  
    } finally {
27  
      thread = null;
28  
    }
29  
  }
30  
  
31  
  close {
32  
    closed.raise();
33  
    interruptThread(thread);
34  
  }
35  
  
36  
  ReferenceQueue queue aka get() { ret queue; }
37  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035343
Snippet name: RunnablesReferenceQueue - a reference queue for Reference objects that implement Runnable (will be run when enqueued)
Eternal ID of this version: #1035343/8
Text MD5: ad201c5e28da5e4b4a5c0b7d0a090d73
Transpilation MD5: a94b06f65bbed36ed583bc03332336e3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-01 19:11:37
Source code size: 874 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 142 / 261
Version history: 7 change(s)
Referenced in: [show references]