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

39
LINES

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

JavaX fragment (include)

1  
sclass QPool {
2  
  replace Q with CompactQ.
3  
  
4  
  int max = numberOfCores();
5  
  new SelfNotifyingSet<Q> used;
6  
  new SelfNotifyingSet<Q> free;
7  
  
8  
  synchronized int total() { ret l(used)+l(free); }
9  
  synchronized bool nonFree() { ret empty(free) && max < total(); }
10  
  
11  
  event customerMustWaitAlert;
12  
  
13  
  void fireCustomerMustWaitAlert {
14  
    vmBus_send customerMustWaitAlert(this, currentQ());
15  
    customerMustWaitAlert();
16  
  }
17  
  
18  
  synchronized WithAutoCloseable<Q> acquireQ() {
19  
    if (nonFree())
20  
      fireCustomerMustWaitAlert();
21  
    if (total() < max)
22  
      used.add(newQ());
23  
    sleepWhileEmpty(free);
24  
    Q t = assertNotNull(popFirst(free));
25  
    used.add(t);
26  
    ret withAutoCloseable(-> giveQBack(t), t);
27  
  }
28  
  
29  
  synchronized void giveQBack(Q t) {
30  
    used.remove(t);
31  
    free.add(t);
32  
  }
33  
  
34  
  static Q newQ() {
35  
    ret new Q("Q Pool Inhabitant " + n2(total()+1)) {
36  
      
37  
    };
38  
  }
39  
}

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