-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCar.java
More file actions
61 lines (47 loc) · 1.5 KB
/
Copy pathCar.java
File metadata and controls
61 lines (47 loc) · 1.5 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
class Bridge {
private int capacity;
private String[] vehicles;
private int count;
public Bridge(int capacity) {
this.capacity = capacity;
this.vehicles = new String[capacity];
this.count = 0;
}
public synchronized void cross(String vehicleName) {
while (count >= capacity) {
System.out.println(vehicleName + " is waiting to cross the bridge.");
}
vehicles[count] = vehicleName;
count++;
System.out.println(vehicleName + " is crossing the bridge.");
for(int i=0;i<1000;i++);
count--;
System.out.println(vehicleName + " has crossed the bridge.");
// Shift vehicles in the array to maintain order (if needed)
for (int i = 0; i < count; i++) {
vehicles[i] = vehicles[i + 1];
}
vehicles[count] = null;
}
}
class Vehicle implements Runnable {
private String name;
private Bridge bridge;
public Vehicle(String name, Bridge bridge) {
this.name = name;
this.bridge = bridge;
}
@Override
public void run() {
bridge.cross(name);
}
}
public class Car {
public static void main(String[] args) {
Bridge bridge = new Bridge(2); // Example capacity of 2 vehicles
for (int i = 1; i <= 5; i++) {
Thread vehicleThread = new Thread(new Vehicle("Vehicle " + i, bridge));
vehicleThread.start();
}
}
}