Libraryless. Click here for Pure Java version (4527L/25K).
// Note: This does have the values problem (complicated values can cause memory leaks) sclass BetterThreadLocal<A> { Map<Thread, A> map = newWeakHashMap(); *() {} *(A value) { set(value); } bool isSet() { ret map.containsKey(currentThread()); } A get() { if (map.containsKey(currentThread())) ret map.get(currentThread()); A value = initialValue(); set(value); ret value; } A get(Thread thread) { ret thread == null ? null : map.get(thread); } void set(A a) { map.put(currentThread(), a); } public A initialValue() { null; } }
Also this can give you the value for another thread.
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1011383 |
Snippet name: | BetterThreadLocal - no memory leaks. sadly (but I bet it's _really_ ok! Has anyone checked whether it makes any difference in practice?) a bit slower than ThreadLocal |
Eternal ID of this version: | #1011383/13 |
Text MD5: | 0118518547504817300fd4ca958cad1e |
Transpilation MD5: | c31e697507eacc906cac33150ab4679c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-11-01 23:53:35 |
Source code size: | 622 bytes / 27 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 601 / 1869 |
Version history: | 12 change(s) |
Referenced in: | [show references] |