Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to write Java Program using the concept of Multithreading? Write a JAVA prog

ID: 2246528 • Letter: H

Question

How to write Java Program using the concept of Multithreading?

Write a JAVA program that would simulate traffic lights using the concept of Multithreading. One example is shown in the following figure. Each light has two statuses, "on" amd "off". Moreover, the time of each light switching status can be set using the text input box beforehand. If the user clicks on the "Start" button, the program starts to work. While if the "End" button is clicked, the program stops working but not terminates. The program terminates only when the "X" button on right-upper comer. Please note that, initially the switching time for each light would be set to "3". As shown in the last column of following fiure, you should also display the text indicating the status of traffic light. Truffic ight demo - Creen

Explanation / Answer

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TrafficLight extends JFrame implements Runnable

{

JButton red, green, yellow ;

TrafficLight()

{

setTitle("TrafficLight") ;

setSize(500,500) ;

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

red = new JButton() ;

yellow = new JButton() ;

green = new JButton() ;

setLayout(new GridLayout(3,1));

this.add(red); this.add(yellow); this.add(green) ;

Thread t = new Thread(this) ;

t.start();

}

public void run()

{

while(true)

{

Thread turnRed = new Thread(new TurnRed());

turnRed.start() ;

synchronized(turnRed)

{

try { turnRed.wait() ; } catch(InterruptedException e) {}

}

Thread turnYellow = new Thread(new TurnYellow());

turnYellow.start() ;

synchronized(turnYellow)

{

try { turnYellow.wait() ; } catch(InterruptedException e) {}

}

Thread turnGreen = new Thread(new TurnGreen());

turnGreen.start() ;

synchronized(turnGreen)

{

try { turnGreen.wait() ; } catch(InterruptedException e) {}

}

}

}

public static void main(String[] args)

{

new TrafficLight().setVisible(true);

}

class TurnRed implements Runnable

{

public void run()

{

synchronized(this) {

green.setBackground(Color.WHITE) ;

red.setBackground(Color.RED) ;

try { Thread.sleep(1000); } catch(InterruptedException e) {}

notify(); }

}

}

class TurnYellow implements Runnable

{

public void run()

{

synchronized(this) {

red.setBackground(Color.WHITE) ;

yellow.setBackground(Color.YELLOW) ;

try { Thread.sleep(1000); } catch(InterruptedException e) {}

notify(); }

}

}

class TurnGreen implements Runnable

{

public void run()

{

synchronized(this) {

yellow.setBackground(Color.WHITE) ;

green.setBackground(Color.GREEN) ;

try { Thread.sleep(1000); } catch(InterruptedException e) {}

notify(); }

}

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote