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

14
LINES

< > BotCompany Repo | #1035581 // AtomicCounter

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

Libraryless. Click here for Pure Java version (67L/1K).

sclass AtomicCounter {
  volatile int counter;
  
  // statically get var handles
  private static final VarHandle vhCounter;
  static ctex {
    var l = MethodHandles.lookup();
    vhCounter = l.findVarHandle(AtomicCounter.class, "counter", int.class);
  }
  
  int incCounter aka inc() {
    ret (int) vhCounter.getAndAdd(this, 1)+1;
  }
}

Author comment

Began life as a copy of #1035570

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035581
Snippet name: AtomicCounter
Eternal ID of this version: #1035581/4
Text MD5: 5ad88a61b8bf1d5064d05ce8a6382365
Transpilation MD5: 6319db0f46663e9496dd8bbbf677d863
Author: stefan
Category: javax / parallelism
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-19 04:00:50
Source code size: 354 bytes / 14 lines
Pitched / IR pitched: No / No
Views / Downloads: 15 / 45
Version history: 3 change(s)
Referenced in: [show references]