-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection.java
More file actions
71 lines (60 loc) · 2.42 KB
/
Copy pathIntersection.java
File metadata and controls
71 lines (60 loc) · 2.42 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
/* COMP2240 Assignment 2
* File: Intersection.java
* Author: Nicholas Steuart c3330826
* Date Created: 9/9/24
* Date Last Modified: 27/9/24
* Description: Implements the functionality of the Intersection in the Problem 1. It utilises Semaphores to prevent
* collisions, deadlock, livelock and starvation of MAC operations.
*/
// PACKAGES //
import java.util.concurrent.Semaphore;
public class Intersection
{
// CLASS VARIABLES //
private final Semaphore semaphore; //Semaphore used to ensure mutual exclusion
private int trail1Count = 0, trail2Count = 0; //Counts the total amount of times trail 1 and trail 2 have been crossed respectively
// CONSTRUCTORS //
//PRE-CONDITION: No pre-conditions
//POST-CONDITION: Default Constructor instantiated
public Intersection()
{
semaphore = null;
}
//PRE-CONDITION: No pre-conditions
//POST-CONDITION: Specialised Constructor instantiated with Parameters permit parsed into a newly instantiated Semaphore to determine it's value
public Intersection(int permit)
{
this.semaphore = new Semaphore(permit);
}
// METHODS //
//PRE-CONDITION: Intersection Class instantiated with an instantiated Semaphore
//POST-CONDITION: Either returns true if the semaphore has been acquired by a Thread or false if the Thread was interrupted.
public boolean enterIntersection() throws InterruptedException
{
try
{
semaphore.acquire();
return true;
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
return false;
}
}
//PRE-CONDITION: Intersection Class instantiated with an instantiated Semaphore
//POST-CONDITION: Returns true
public boolean exitIntersection()
{
semaphore.release(); //Semaphore signals its release
return true;
}
//PRE-CONDITION: Parameter trail must be a String of the value "ED1", "ED2", "CSR1", or "CSR2"
//POST-CONDITION: Returns a String containing the updated trail completion information
public String updateTrailCompletions(String trail)
{
if(trail.charAt(trail.length() - 1) == '1') trail1Count++; //IF trail ends in a 1, increment trail 1 count, else increment trail 2 count
else trail2Count++;
return "Total crossed in Trail1: " + trail1Count + " Trail2: " + trail2Count;
}
}