-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fork.java
40 lines (32 loc) · 1.03 KB
/
Fork.java
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
/*
* @author: Scott C Gramig
* @program: Fork class for Dining Philosophers Problem
* @note: Use in combination with DiningPhilosophers.java and Philosopher.java
*/
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JLabel;
public class Fork extends ReentrantLock{
//class attributes
int id;// fork ID
JLabel label;// fork label
//Fork Constructor
public Fork(int id, JLabel label) {
super();
this.id = id;
this.label = label;
}
//method that allows the Philosopher to pick up the fork
public synchronized void pickUp(){
//locking the fork to ensure unique access
this.lock();
//updating the fork label on the GUI
label.setIcon(DiningPhilosophers.forkUsedIcons[id]);
}
//method that allows the Philosopher to pick up the fork
public synchronized void putDown(){
//unlocking the fork to make it available for the neighbor Philosopher to use
this.unlock();
//updating the fork label on the GUI
label.setIcon(DiningPhilosophers.forkAvailableIcons[id]);
}
}