/** thread safe */
class Counter {
/** guarded by 'lock' */
private long count;
private final Object lock = new Object();
void increment(long amount) {
if (amount < 1) {
throw new IllegalArgumentException("Only increments!");
}
synchronized (this.lock) {
this.count += amount;
}
}
}
/** thread safe */
class Counter {
/** guarded by 'lock' */
private long count;
private final ReentrantLock lock = new ReentrantLock(true);
void increment(long amount) {
if (amount < 1) {
throw new IllegalArgumentException("Only increments!");
}
this.lock.lock();
try {
this.count += amount;
} finally {
this.lock.unlock();
}
}
}
synchronized
true
parameter in the constructor), real fcfs