-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommute.java
More file actions
112 lines (93 loc) · 2.24 KB
/
Copy pathCommute.java
File metadata and controls
112 lines (93 loc) · 2.24 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collegetransportapplication;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author soundarya
*/
public class Commute {
private int numStudents;
public int busCapacity;
public int numCoordinators;
public int busGroup = 0;
private int waitingBusStudents = 0;
private int waitingCoordStudents = 0;
public Object coord = new Object();
public Object aGroup = new Object();
public Commute(int numStudents, int busCapacity, int numCoordinators)
{
this.numStudents = numStudents;
this.busCapacity = busCapacity;
this.numCoordinators= numCoordinators;
}
public synchronized int getnumStudents() {
return numStudents;
}
public synchronized void decrementnumStudents() {
numStudents--;
}
//checks if a group is ready for boarding bus
public synchronized boolean groupReady()
{
if(busGroup >= busCapacity || (waitingBusStudents == 0 && busGroup > 0))
return true;
else return false;
}
public void findBus() //for bussed students
{
waitingBusStudents++;
while(true) {
if(!groupReady())
synchronized(aGroup)
{
waitingBusStudents--;
busGroup++;
try {
aGroup.wait(); return; }
catch (InterruptedException e) {
System.out.println("Something bad happened.");
}
}
}
}
public void findCoordinator() //for car students
{
waitingCoordStudents++;
synchronized(coord)
{
try {
coord.wait();
waitingCoordStudents--;
return;
}
catch (InterruptedException e) {
System.out.println("Something bad happened.");
}
}
}
public synchronized void busPickUp() //for bus
{
if(groupReady())
synchronized(aGroup)
{
//notify the current group
aGroup.notifyAll();
busGroup = 0;
}
}
public synchronized void pickUpStudents() //for coordinator
{
synchronized(coord)
{
coord.notify();
}
}
public synchronized int getWaitingCoordStudents() {
return waitingCoordStudents;
}
}