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

40
LINES

< > BotCompany Repo | #1032124 // SerializeComputations

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

Libraryless. Click here for Pure Java version (6332L/36K).

1  
sclass SerializeComputations<A, B> {
2  
  Map<A, Var<OKOrError<B>>> calculating = syncMap();
3  
  new MultiSetMap<A, Thread> waitingFor;
4  
  
5  
  B get(A a, IF1<A, B> f) {
6  
    ret get(a, () -> f.get(a));
7  
  }
8  
9  
  B get(A a, IF0<B> f) {
10  
    bool meCalc;
11  
    Var<OKOrError<B>> var;
12  
    synchronized {
13  
      var = calculating.get(a);
14  
      meCalc = var == null;
15  
      
16  
      if (meCalc) {
17  
        var = new Var;
18  
        calculating.put(a, var);
19  
      } else
20  
        waitingFor.put(a, currentThread());
21  
    }
22  
    
23  
    if (meCalc) {
24  
      OKOrError<B> b = okOrError(() -> f!);
25  
      var.set(b);
26  
      ret b.getMandatory();
27  
    } else {
28  
      try {
29  
        waitUntilNotNull(var);
30  
        ret var->getMandatory();
31  
      } finally {
32  
        synchronized {
33  
          waitingFor.remove(a, currentThread());
34  
          if (!waitingFor.containsKey(a))
35  
            calculating.remove(a);
36  
        }
37  
      }
38  
    }
39  
  }
40  
}

Author comment

Began life as a copy of #1032122

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032124
Snippet name: SerializeComputations
Eternal ID of this version: #1032124/11
Text MD5: dc0dfda23739f460c10dc6f43fd0c864
Transpilation MD5: 01c17d6a75ebecebb251938a01470071
Author: stefan
Category: javax / multi-threading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-01 23:22:20
Source code size: 930 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 200 / 420
Version history: 10 change(s)
Referenced in: [show references]