forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountWithSyncUsingLock.java
More file actions
executable file
·69 lines (66 loc) · 2 KB
/
AccountWithSyncUsingLock.java
File metadata and controls
executable file
·69 lines (66 loc) · 2 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* */ package ch32;
/* */ import java.util.concurrent.ExecutorService;
/* */ import java.util.concurrent.Executors;
/* */ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/* */
/* */ public class AccountWithSyncUsingLock {
/* 7 */ private static Account account = new Account();
/* */
/* */ public static void main(String[] args) {
/* 10 */ ExecutorService executor = Executors.newCachedThreadPool();
/* */
/* */
/* 13 */ for (int i = 0; i < 100; i++) {
/* 14 */ executor.execute(new AddAPennyTask());
/* */ }
/* */
/* 17 */ executor.shutdown();
/* */
/* */
/* 20 */ while (!executor.isTerminated());
/* */
/* */
/* 23 */ System.out.println("What is balance ? " + account.getBalance());
/* */ }
/* */
/* */ public static class AddAPennyTask
/* */ implements Runnable {
/* */ public void run() {
/* 29 */ AccountWithSyncUsingLock.account.deposit(1);
/* */ }
/* */ }
/* */
/* */ public static class Account
/* */ {
/* 35 */ private static Lock lock = new ReentrantLock();
/* 36 */ private int balance = 0;
/* */
/* */ public int getBalance() {
/* 39 */ return this.balance;
/* */ }
/* */
/* */ public void deposit(int amount) {
/* 43 */ lock.lock();
/* */
/* */ try {
/* 46 */ int newBalance = this.balance + amount;
/* */
/* */
/* */
/* 50 */ Thread.sleep(5L);
/* */
/* 52 */ this.balance = newBalance;
/* */ }
/* 54 */ catch (InterruptedException interruptedException) {
/* */
/* */ } finally {
/* 57 */ lock.unlock();
/* */ }
/* */ }
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch32/AccountWithSyncUsingLock.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/