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).

// Note: Costs one thread while it exists
sclass RunnablesReferenceQueue is AutoCloseable {
  new ReferenceQueue queue;
  volatile Thread thread;
  new Flag closed;
  
  settable int timeout = 60000; // just for safety
  
  *() {
    thread = startThread("RunnablesReferenceQueue", l0 _run);
  }
  
  void _run() ctex {
    try {
      while (ping() && !closed!) {
        Reference<?> ref = queue.remove(timeout);
        if (ref != null) {
          if (ref cast Runnable)
            pcallF(ref);
          else
            warn("RunnablesReferenceQueue: Reference not runnable - "
              + className(ref));
        }
      }
    } catch InterruptedException e {
    } finally {
      thread = null;
    }
  }
  
  close {
    closed.raise();
    interruptThread(thread);
  }
  
  ReferenceQueue queue aka get() { ret queue; }
}

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: 81 / 169
Version history: 7 change(s)
Referenced in: [show references]