Rename classes
This commit is contained in:
parent
6534bddb6c
commit
4556829955
|
@ -3,32 +3,32 @@ package com.baeldung.atomicstampedreference;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicStampedReference;
|
import java.util.concurrent.atomic.AtomicStampedReference;
|
||||||
|
|
||||||
public class SafeAccount {
|
public class StampedAccount {
|
||||||
|
|
||||||
private AtomicInteger stamp = new AtomicInteger(0);
|
private AtomicInteger stamp = new AtomicInteger(0);
|
||||||
private AtomicStampedReference<Integer> balance = new AtomicStampedReference<>(0, 0);
|
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
|
||||||
|
|
||||||
public int getBalance() {
|
public int getBalance() {
|
||||||
return this.balance.get(new int[1]);
|
return this.account.get(new int[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getStamp() {
|
public int getStamp() {
|
||||||
int[] stamps = new int[1];
|
int[] stamps = new int[1];
|
||||||
this.balance.get(stamps);
|
this.account.get(stamps);
|
||||||
return stamps[0];
|
return stamps[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean deposit(int funds) {
|
public boolean deposit(int funds) {
|
||||||
int[] stamps = new int[1];
|
int[] stamps = new int[1];
|
||||||
int current = this.balance.get(stamps);
|
int current = this.account.get(stamps);
|
||||||
int newStamp = this.stamp.incrementAndGet();
|
int newStamp = this.stamp.incrementAndGet();
|
||||||
return this.balance.compareAndSet(current, current + funds, stamps[0], newStamp);
|
return this.account.compareAndSet(current, current + funds, stamps[0], newStamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean withdrawal(int funds) {
|
public boolean withdrawal(int funds) {
|
||||||
int[] stamps = new int[1];
|
int[] stamps = new int[1];
|
||||||
int current = this.balance.get(stamps);
|
int current = this.account.get(stamps);
|
||||||
int newStamp = this.stamp.incrementAndGet();
|
int newStamp = this.stamp.incrementAndGet();
|
||||||
return this.balance.compareAndSet(current, current - funds, stamps[0], newStamp);
|
return this.account.compareAndSet(current, current - funds, stamps[0], newStamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,11 +3,11 @@ package com.baeldung.atomicstampedreference;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ThreadSafeAccountUnitTest {
|
public class ThreadStampedAccountUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultiThread_whenSafeAccount_thenSetBalance() throws InterruptedException {
|
public void givenMultiThread_whenStampedAccount_thenSetBalance() throws InterruptedException {
|
||||||
SafeAccount account = new SafeAccount();
|
StampedAccount account = new StampedAccount();
|
||||||
Thread t = new Thread(() -> {
|
Thread t = new Thread(() -> {
|
||||||
while (!account.withdrawal(100))
|
while (!account.withdrawal(100))
|
||||||
Thread.yield();
|
Thread.yield();
|
Loading…
Reference in New Issue