multithreaded Java program

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a multithreaded Java program that uses synchronized methods (Section 7.4.1), reentrant locks (Section 7.4.2), or semaphores (Section 7.4.3) to control access to a one- lane bridge. One thread should simulate westbound vehicles and another thread should simulate eastbound vehicles. The vehicles do not have a reverse gear, so the bridge would become deadlocked if both a westbound vehicle and an eastbound vehicle were allowed to drive onto the bridge at the same time. Therefore, mutual exclusion must be enforced on the use of the bridge. Your solution should avoid both deadlock and starvation (e.g. the bridge being monopolized by westbound vehicles while the eastbound vehicles never get to cross). Vehicles traveling in either direction should wait (sleep) for some amount of time, then attempt to cross. Once a vehicle is on the bridge, it should sleep for some amount of time to simulate how long it takes to drive across the bridge.

Output a message when each vehicle drives onto the bridge and another message when that vehicle has completed the crossing. Simulate several (at least five) vehicles traveling in each direction.

 

Per the images below, I have most of the code finished. But for some reason the output is only giving me the threads entering the bridge sequentially and not from both sides. There is an issue with the mutual exclusion that I cannot figure out. Please help.

O SynchronizedTrafficTester.java > q$ SynchronizedTrafficTester > O main(String[])
import java.util.concurrent.Semaphore;
1.
2
public class SynchronizedTrafficTester {
4
Run | Debug
public static void main(String [] args) {
final int NUM_CARS = 5;
//SynchronizedTraffic syncTraf = new SynchronizedTraffic();
Semaphore bridge
6
9
new Semaphore(1);
10
Thread [] eastBoundCars = new Thread [NUM_CARS]; //creates an array of threads for east bound cars
for (int i = 0; i < eastBoundCars.length; i++) {
11
12
13
eastBoundCars [i]
new Thread (new Car());
//5 cars in the threads
eastBoundCars [i].start();
14
15
16
Thread [] westBoundCars = new Thread [NUM_CARS]; //creates an array of threads for west bound cars
for (int i = 0; i < westBoundCars.length; i++) {
17
18
19
westBoundCars[i]
new Thread (new Car());
//5 cars in the threads
20
westBoundCars [i].start();
21
22
23
24
25
26
}
27
}
28
Transcribed Image Text:O SynchronizedTrafficTester.java > q$ SynchronizedTrafficTester > O main(String[]) import java.util.concurrent.Semaphore; 1. 2 public class SynchronizedTrafficTester { 4 Run | Debug public static void main(String [] args) { final int NUM_CARS = 5; //SynchronizedTraffic syncTraf = new SynchronizedTraffic(); Semaphore bridge 6 9 new Semaphore(1); 10 Thread [] eastBoundCars = new Thread [NUM_CARS]; //creates an array of threads for east bound cars for (int i = 0; i < eastBoundCars.length; i++) { 11 12 13 eastBoundCars [i] new Thread (new Car()); //5 cars in the threads eastBoundCars [i].start(); 14 15 16 Thread [] westBoundCars = new Thread [NUM_CARS]; //creates an array of threads for west bound cars for (int i = 0; i < westBoundCars.length; i++) { 17 18 19 westBoundCars[i] new Thread (new Car()); //5 cars in the threads 20 westBoundCars [i].start(); 21 22 23 24 25 26 } 27 } 28
O Car.java > q Car
import java.util.concurrent.Semaphore;
2
public class Car implements Runnable {
Semaphore bridge = new Semaphore(1); |
7
public void run() {
try {
bridge.acquire(); //acquires semaphore, providing mutual exclusion
System.out.println("Car: " + Thread.currentThread ().getName () + " has enterred the bridge.");
8
9
10
11
Thread.sleep(3000); //sleep to simulate crossing the bridge
Thread.currentThread ().getName () + " has successfully crossed the bridge and exited.");
System.out.println("Car: "
} catch (Exception e) {
//TODO: handle exception
//if busy, blocked
} finally {
bridge.release();
12
13
14
15
16
17
//placed in finally to ensure that semaphore is released
18
}
19
}
20
21
22
Transcribed Image Text:O Car.java > q Car import java.util.concurrent.Semaphore; 2 public class Car implements Runnable { Semaphore bridge = new Semaphore(1); | 7 public void run() { try { bridge.acquire(); //acquires semaphore, providing mutual exclusion System.out.println("Car: " + Thread.currentThread ().getName () + " has enterred the bridge."); 8 9 10 11 Thread.sleep(3000); //sleep to simulate crossing the bridge Thread.currentThread ().getName () + " has successfully crossed the bridge and exited."); System.out.println("Car: " } catch (Exception e) { //TODO: handle exception //if busy, blocked } finally { bridge.release(); 12 13 14 15 16 17 //placed in finally to ensure that semaphore is released 18 } 19 } 20 21 22
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY