Skip to content

Commit

Permalink
Feat: Atom
Browse files Browse the repository at this point in the history
  • Loading branch information
currenjin committed Jan 21, 2025
1 parent c23d088 commit cc77a47
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.currenjin.concurrency;

public class Atom {
private volatile int value;

public Atom(int value) {
this.value = value;
}

public final int incrementAndGet() {
while(true) {
int current = value;
int next = current + 1;
if (compareAndSet(current, next)) {
return next;
}
}
}

private boolean compareAndSet(int expected, int newValue) {
return true;
// CPU ๋ช…๋ น
// return unsafe.compareAndSwapInt(this, valueOffset, expected, newValue);
}
}

0 comments on commit cc77a47

Please sign in to comment.