forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadCooperation.java
More file actions
executable file
·103 lines (100 loc) · 3.2 KB
/
ThreadCooperation.java
File metadata and controls
executable file
·103 lines (100 loc) · 3.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* */ package ch32;
/* */ import java.util.concurrent.ExecutorService;
/* */ import java.util.concurrent.Executors;
/* */ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/* */
/* */ public class ThreadCooperation {
/* 7 */ private static Account account = new Account();
/* */
/* */
/* */ public static void main(String[] args) {
/* 11 */ ExecutorService executor = Executors.newFixedThreadPool(2);
/* 12 */ executor.execute(new DepositTask());
/* 13 */ executor.execute(new WithdrawTask());
/* 14 */ executor.shutdown();
/* */
/* 16 */ System.out.println("Thread 1\t\tThread 2\t\tBalance");
/* */ }
/* */
/* */ public static class DepositTask
/* */ implements Runnable {
/* */ public void run() {
/* */ try {
/* */ while (true) {
/* 24 */ ThreadCooperation.account.deposit((int)(Math.random() * 10.0D) + 1);
/* 25 */ Thread.sleep(1000L);
/* */ }
/* */
/* 28 */ } catch (InterruptedException ex) {
/* 29 */ ex.printStackTrace();
/* */ return;
/* */ }
/* */ }
/* */ }
/* */
/* */ public static class WithdrawTask implements Runnable {
/* */ public void run() {
/* */ while (true) {
/* 38 */ ThreadCooperation.account.withdraw((int)(Math.random() * 10.0D) + 1);
/* */ }
/* */ }
/* */ }
/* */
/* */
/* */ private static class Account
/* */ {
/* 46 */ private static Lock lock = new ReentrantLock();
/* */
/* */
/* 49 */ private static Condition newDeposit = lock.newCondition();
/* */
/* 51 */ private int balance = 0;
/* */
/* */ public int getBalance() {
/* 54 */ return this.balance;
/* */ }
/* */
/* */ public void withdraw(int amount) {
/* 58 */ lock.lock();
/* */ try {
/* 60 */ while (this.balance < amount) {
/* 61 */ System.out.println("\t\t\tWait for a deposit");
/* 62 */ newDeposit.await();
/* */ }
/* */
/* 65 */ this.balance -= amount;
/* 66 */ System.out.println("\t\t\tWithdraw " + amount + "\t\t" +
/* 67 */ getBalance());
/* */ }
/* 69 */ catch (InterruptedException ex) {
/* 70 */ ex.printStackTrace();
/* */ } finally {
/* */
/* 73 */ lock.unlock();
/* */ }
/* */ }
/* */
/* */ public void deposit(int amount) {
/* 78 */ lock.lock();
/* */ try {
/* 80 */ this.balance += amount;
/* 81 */ System.out.println("Deposit " + amount + "\t\t\t\t\t" +
/* 82 */ getBalance());
/* */
/* */
/* 85 */ newDeposit.signalAll();
/* */ } finally {
/* */
/* 88 */ lock.unlock();
/* */ }
/* */ }
/* */
/* */ private Account() {}
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch32/ThreadCooperation.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/