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

39
LINES

< > BotCompany Repo | #1033405 // QPool [dev.]

JavaX fragment (include)

sclass QPool {
  replace Q with CompactQ.
  
  int max = numberOfCores();
  new SelfNotifyingSet<Q> used;
  new SelfNotifyingSet<Q> free;
  
  synchronized int total() { ret l(used)+l(free); }
  synchronized bool nonFree() { ret empty(free) && max < total(); }
  
  event customerMustWaitAlert;
  
  void fireCustomerMustWaitAlert {
    vmBus_send customerMustWaitAlert(this, currentQ());
    customerMustWaitAlert();
  }
  
  synchronized WithAutoCloseable<Q> acquireQ() {
    if (nonFree())
      fireCustomerMustWaitAlert();
    if (total() < max)
      used.add(newQ());
    sleepWhileEmpty(free);
    Q t = assertNotNull(popFirst(free));
    used.add(t);
    ret withAutoCloseable(-> giveQBack(t), t);
  }
  
  synchronized void giveQBack(Q t) {
    used.remove(t);
    free.add(t);
  }
  
  static Q newQ() {
    ret new Q("Q Pool Inhabitant " + n2(total()+1)) {
      
    };
  }
}

Author comment

Began life as a copy of #1033401

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

-
Snippet ID: #1033405
Snippet name: QPool [dev.]
Eternal ID of this version: #1033405/1
Text MD5: ff103aa802d330a34b083b3de55a3252
Author: stefan
Category: javax / code execution
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-27 04:45:06
Source code size: 926 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 137 / 152
Referenced in: