added revision number and cas failure count
This commit is contained in:
parent
fd32bd2f4d
commit
12ef94be39
@ -1,39 +1,53 @@
|
|||||||
package com.baeldung.abaproblem;
|
package com.baeldung.abaproblem;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class Account {
|
public class Account {
|
||||||
|
|
||||||
private AtomicInteger balance = new AtomicInteger(0);
|
private AtomicInteger balance;
|
||||||
private List<Long> transactionDates = new ArrayList<>();
|
private AtomicInteger transactionCount;
|
||||||
|
private ThreadLocal<Integer> currentThreadCASFailureCount;
|
||||||
|
|
||||||
|
public Account() {
|
||||||
|
this.balance = new AtomicInteger(0);
|
||||||
|
this.transactionCount = new AtomicInteger(0);
|
||||||
|
this.currentThreadCASFailureCount = new ThreadLocal<>();
|
||||||
|
this.currentThreadCASFailureCount.set(0);
|
||||||
|
}
|
||||||
|
|
||||||
public int getBalance() {
|
public int getBalance() {
|
||||||
return balance.get();
|
return balance.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Long> getTransactionDates() {
|
public int getTransactionCount() {
|
||||||
return transactionDates;
|
return transactionCount.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean withdraw(int amount) throws InterruptedException {
|
public int getCurrentThreadCASFailureCount() {
|
||||||
int current = getBalance();
|
return currentThreadCASFailureCount.get();
|
||||||
if (current < amount) {
|
|
||||||
throw new RuntimeException("Not sufficient balance");
|
|
||||||
}
|
}
|
||||||
precessBalance();
|
|
||||||
|
public boolean withdraw(int amount) {
|
||||||
|
int current = getBalance();
|
||||||
|
maybeWait();
|
||||||
boolean result = balance.compareAndSet(current, current - amount);
|
boolean result = balance.compareAndSet(current, current - amount);
|
||||||
if (result) {
|
if (result) {
|
||||||
transactionDates.add(System.currentTimeMillis());
|
transactionCount.incrementAndGet();
|
||||||
|
} else {
|
||||||
|
int currentCASFailureCount = currentThreadCASFailureCount.get();
|
||||||
|
currentThreadCASFailureCount.set(currentCASFailureCount + 1);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void precessBalance() throws InterruptedException {
|
private void maybeWait() {
|
||||||
if ("thread1".equals(Thread.currentThread().getName())) {
|
if ("thread1".equals(Thread.currentThread().getName())) {
|
||||||
|
try {
|
||||||
TimeUnit.SECONDS.sleep(2);
|
TimeUnit.SECONDS.sleep(2);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +55,10 @@ public class Account {
|
|||||||
int current = balance.get();
|
int current = balance.get();
|
||||||
boolean result = balance.compareAndSet(current, current + amount);
|
boolean result = balance.compareAndSet(current, current + amount);
|
||||||
if (result) {
|
if (result) {
|
||||||
transactionDates.add(System.currentTimeMillis());
|
transactionCount.incrementAndGet();
|
||||||
|
} else {
|
||||||
|
int currentCASFailureCount = currentThreadCASFailureCount.get();
|
||||||
|
currentThreadCASFailureCount.set(currentCASFailureCount + 1);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user