Libraryless. Click here for Pure Java version (96L/1K).
import java.lang.invoke.VarHandle; import java.lang.invoke.MethodHandles; // assumes that each set function is called only once sclass PairCollector<A, B> { volatile A a; volatile B b; volatile int counter; // statically get var handles private static final VarHandle vhA, vhB, vhCounter; static ctex { var l = MethodHandles.lookup(); vhA = l.findVarHandle(PairCollector.class, "a", O); vhB = l.findVarHandle(PairCollector.class, "b", O); vhCounter = l.findVarHandle(PairCollector.class, "counter", int.class); } void setA(A a) { this.a = a; incCounter(); } void setB(B b) { this.b = b; incCounter(); } IVF1<A> setterForA() { ret a -> setA(a); } IVF1<B> setterForB() { ret b -> setB(b); } private void incCounter() { if (((int) vhCounter.getAndAdd(this, 1))+1 == 2) complete(a, b); } // can override or swap swappable void complete(A a, B b) {} }
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035562 |
Snippet name: | PairCollector - concurrent collector for 2 objects with overridable & swappable complete() function |
Eternal ID of this version: | #1035562/11 |
Text MD5: | 14311b666f416b8e2cc16bb23f836290 |
Transpilation MD5: | d9bdac352b1794c567bb83f8d576fc77 |
Author: | stefan |
Category: | javax / parallelism |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-06-17 00:26:12 |
Source code size: | 949 bytes / 32 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 166 / 262 |
Version history: | 10 change(s) |
Referenced in: | [show references] |