[Go to site: main page, start]

0% found this document useful (0 votes)
9 views5 pages

Complex Problem (Java Multithreading)

The document contains Java code for two solutions: a producer-consumer problem using a shared buffer and a traffic light simulation for cars at an intersection. The first solution demonstrates synchronized methods for producing and consuming items in a queue, while the second solution manages traffic flow by allowing cars to pass based on the traffic light's state. Both implementations utilize threading and synchronization to handle concurrent operations effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Complex Problem (Java Multithreading)

The document contains Java code for two solutions: a producer-consumer problem using a shared buffer and a traffic light simulation for cars at an intersection. The first solution demonstrates synchronized methods for producing and consuming items in a queue, while the second solution manages traffic flow by allowing cars to pass based on the traffic light's state. Both implementations utilize threading and synchronization to handle concurrent operations effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name : Priyanshu Katoch

UID:24MCI10207
Complex Problem {Day-1}

Solu on 1 :
import java.u [Link];
import java.u [Link];

class SharedBuffer {
private final Queue<Integer> queue = new LinkedList<>();
private final int CAPACITY = 5;

public synchronized void produce(int value) throws InterruptedExcep on {


while ([Link]() == CAPACITY) {
wait();
}
queue.offer(value);
[Link]("Produced: " + value);
no fy();
}

public synchronized int consume() throws InterruptedExcep on {


while ([Link]()) {
wait();
}
int value = [Link]();
[Link]("Consumed: " + value);
no fy();
return value;
}
}

class Producer extends Thread {


private final SharedBuffer buffer;

public Producer(SharedBuffer buffer) {


[Link]ffer = buffer;
}

public void run() {


try {
for (int i = 1; i <= 5; i++) {
buff[Link](i); [Link](300); //
Shorter sleep for online compilers
}
} catch (InterruptedExcep on e) {
[Link]("Producer interrupted");
}
}
}

class Consumer extends Thread {


private final SharedBuffer buffer;

public Consumer(SharedBuffer buffer) {


[Link]ffer = buffer;
}

public void run() {


try {
for (int i = 1; i <= 5; i++) {
buff[Link]();
[Link](500);
}
} catch (InterruptedExcep on e) {
[Link]("Consumer interrupted");
}
}
}

public class Main {


public sta c void main(String[] args) {
SharedBuffer buffer = new SharedBuffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);

[Link]();
[Link]();

try {
[Link]();
[Link]();
} catch (InterruptedExcep on e) {
[Link]("Main thread interrupted");
}

[Link]("Execu on complete.");
}
}

Output :
Solu on 2:

class TrafficLight {
private String greenRoad = "A"; // Ini ally, Road A has green

// Method for a car to request to pass


public synchronized void passIntersec on(String roadName, int carId) throws
InterruptedExcep on {
while (![Link](greenRoad)) {
[Link]("Car " + carId + " on Road " + roadName + " is wai ng (RED
light).");
wait(); // Wait ll the light turns green for this road
}

[Link]("🚗 Car " + carId + " is passing through Intersec on on Road " +
roadName + " (GREEN light).");
[Link](1000); // Simulate car passing
}

// Method to switch the green light public


synchronized void switchLight() { greenRoad =
[Link]("A") ? "B" : "A";
[Link]("🔄 Traffic light switched! Green light is now for Road " +
greenRoad);
no fyAll(); // Wake up all wai ng cars
}
}

// Car class as a thread class Car


extends Thread { private
TrafficLight trafficLight; private
String roadName; private int
carId;

public Car(TrafficLight tl, String road, int id) {


[Link]fficLight = tl;
[Link] = road; [Link] =
id;
}

public void run() {


try {
traffi[Link] on(roadName, carId);
} catch (InterruptedExcep on e) {
[Link]();
}
}
}

// Main simula on public class


TrafficIntersec on {
public sta c void main(String[] args) throws InterruptedExcep on {
TrafficLight trafficLight = new TrafficLight();

// Simulate cars arriving from both roads


for (int i = 1; i <= 5; i++) {
new Car(trafficLight, "A", i).start(); // Road A cars
new Car(trafficLight, "B", i + 5).start(); // Road B cars
[Link](300); // Cars arriving at intervals
}

// Switch the green light every few seconds


for (int i = 0; i < 5; i++) {
[Link](4000); // Wait before switching
traffi[Link]();
}
}
}

Output :

You might also like