-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccount.java
More file actions
34 lines (28 loc) · 709 Bytes
/
Account.java
File metadata and controls
34 lines (28 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.yurii.salimov.lesson07.task02;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public final class Account {
private int money;
private final Lock lock;
public Account(int money) {
this.money = money;
this.lock = new ReentrantLock();
}
public int get() {
return this.money;
}
public void withdraw(final int amount) {
this.lock.lock();
try {
if (this.money >= amount) {
this.money -= amount;
}
} finally {
this.lock.unlock();
}
}
}