COMP2396
Loading...
Searching...
No Matches
SmithJob.java
Go to the documentation of this file.
1public class SmithJob implements Runnable {
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++) {
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}
void withdraw(int amount)
int getBalance()
String getName()
Definition SmithJob.java:39
BankAccount account
Definition SmithJob.java:2
void run()
Definition SmithJob.java:14
static void main(String[] args)
Definition SmithJob.java:4
void makeWithdrawal(int amount)
Definition SmithJob.java:20