-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerson.java
More file actions
26 lines (24 loc) · 879 Bytes
/
Copy pathPerson.java
File metadata and controls
26 lines (24 loc) · 879 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
// Copyright: Ganesh Samarthyam, 2016; www.codeops.tech
import java.util.concurrent.Semaphore;
// Each Person is an independent thread; but their access to the common resource
// (two ATM machines in the ATM machine room in this case) needs to be synchronized.
class Person extends Thread {
private Semaphore machines;
public Person(Semaphore machines, String name) {
this.machines = machines;
this.setName(name);
this.start();
}
public void run() {
try {
System.out.println(getName() + " waiting to access an ATM machine");
machines.acquire();
System.out.println(getName() + " is accessing an ATM machine");
Thread.sleep(1000); // simulate the time required for withdrawing amount
System.out.println(getName() + " is done using the ATM machine");
machines.release();
} catch(InterruptedException ie) {
System.err.println(ie);
}
}
}