COMP2396
Loading...
Searching...
No Matches
SmithJob.java
Go to the documentation of this file.
1
public
class
SmithJob
implements
Runnable
{
2
private
BankAccount
account
=
new
BankAccount
();
3
4
public
static
void
main
(String[] args) {
5
SmithJob
theJob =
new
SmithJob
();
6
Thread
mrSmith =
new
Thread
(theJob);
7
Thread
mrsSmith =
new
Thread
(theJob);
8
mrSmith.setName(
"Mr. Smith"
);
9
mrsSmith.setName(
"Mrs. Smith"
);
10
mrSmith.start();
11
mrsSmith.start();
12
}
13
14
public
void
run
() {
15
for
(
int
x = 0; x < 2; x++) {
16
makeWithdrawal
(60);
17
}
18
}
19
20
private
void
makeWithdrawal
(
int
amount) {
21
if
(
account
.
getBalance
() >= amount) {
22
System.out.println(
getName
() +
" is about to withdraw"
);
23
try
{
24
System.out.println(
getName
() +
" is going to sleep"
);
25
Thread
.sleep(500);
26
}
catch
(Exception ex) { ex.printStackTrace(); }
27
System.out.println(
getName
() +
" wakes up"
);
28
account
.
withdraw
(amount);
29
System.out.println(
getName
() +
" completes the withdrawal"
);
30
if
(
account
.
getBalance
() < 0) {
31
System.out.println(
"Overdrawn!"
);
32
}
33
}
34
else
{
35
System.out.println(
"Not enough money for "
+
getName
());
36
}
37
}
38
39
private
String
getName
() {
40
return
Thread
.currentThread().getName();
41
}
42
}
BankAccount
Definition
BankAccount.java:1
BankAccount.withdraw
void withdraw(int amount)
Definition
BankAccount.java:4
BankAccount.getBalance
int getBalance()
Definition
BankAccount.java:3
Runnable
SmithJob
Definition
SmithJob.java:1
SmithJob.getName
String getName()
Definition
SmithJob.java:39
SmithJob.account
BankAccount account
Definition
SmithJob.java:2
SmithJob.run
void run()
Definition
SmithJob.java:14
SmithJob.main
static void main(String[] args)
Definition
SmithJob.java:4
SmithJob.makeWithdrawal
void makeWithdrawal(int amount)
Definition
SmithJob.java:20
Thread
chapter11
SmithJob.java
Generated on Sat Dec 7 2024 17:10:09 for COMP2396 by
1.12.0