Reputation: 199
I have a javafx UI that simulates a traffic light. I have three threads for each of the lights which lights up each of their respective lights while turning off the others. I start the threads and use join() so that each thread can light up their respective lights before another thread starts.
When I use the Thread.join(), the UI freezes but I know the threads are running properly because the of test string i put inside the threads executes and waits for appropriate time before another one executes.
Code is as follows -
main method
public void start(Stage primaryStage) {
lightBoard.setArcWidth(10);
lightBoard.setArcHeight(10);
lightBoard.setFill(Color.DARKGREY);
circleGreen.setFill(Color.WHITE);
circleYellow.setFill(Color.WHITE);
circleRed.setFill(Color.WHITE);
Group group = new Group();
group.getChildren().addAll(lightBoard, circleGreen, circleYellow, circleRed);
BorderPane light = new BorderPane(group);
light.setPadding( new Insets(20, 20, 20, 20) );
GridPane layoutControls = new GridPane();
layoutControls.setAlignment(Pos.CENTER);
Button btnStart = new Button("Start");
Button btnStop = new Button("Stop");
layoutControls.add(btnStart, 0, 0);
layoutControls.add(btnStop, 1, 0);
BorderPane root = new BorderPane();
root.setCenter(light);
root.setBottom(layoutControls);
Scene scene = new Scene(root, 380, 250);
primaryStage.setTitle("Traffic Light Simulator");
primaryStage.setScene(scene);
primaryStage.show();
// create and start threads
Runnable lightGreen = new LightGreen();
Runnable lightYellow = new LightYellow();
Runnable lightRed = new LightRed();
Thread threadGreen = new Thread(lightGreen);
Thread threadYellow = new Thread(lightYellow);
Thread threadRed = new Thread(lightRed);
while(true){
threadGreen = new Thread(lightGreen);
threadGreen.start();
try{
threadGreen.join();
}
catch(Exception e){
}
threadYellow = new Thread(lightYellow);
threadYellow.start();
try{
threadYellow.join();
}
catch(Exception e){
}
threadRed = new Thread(lightRed);
threadRed.start();
try{
threadRed.join();
}
catch(Exception e){
}
}
}
thread classes -
class LightGreen implements Runnable {
@Override
public void run() {
lock.lock();
System.out.println("GREEn");
circleGreen.setFill( Color.GREEN );
circleYellow.setFill( Color.WHITE );
circleRed.setFill( Color.WHITE );
lock.unlock();
try {
Thread.sleep(3000L);
} catch (InterruptedException ex) {
}
}
}
class LightYellow implements Runnable {
@Override
public void run() {
lock.lock();
System.out.println("YELLOW");
circleGreen.setFill( Color.WHITE );
circleYellow.setFill( Color.YELLOW );
circleRed.setFill( Color.WHITE );
lock.unlock();
try {
Thread.sleep(3000L);
} catch (InterruptedException ex) {
}
}
}
class LightRed implements Runnable {
@Override
public void run() {
lock.lock();
System.out.println("RED");
circleGreen.setFill( Color.WHITE );
circleYellow.setFill( Color.WHITE );
circleRed.setFill( Color.RED );
lock.unlock();
try {
Thread.sleep(3000L);
} catch (InterruptedException ex) {
}
}
}
How can i resolve this? I have been told Platform.runlater can be used but not sure how to use it. I need to use multithreading for this task.
Upvotes: 0
Views: 831
Reputation: 18792
Here is a quick demo of how to use JavaFx PauseTransition to update gui:
public class TrafficLight extends Application{
private static final double RADIUS = 50;
private static final double PAUSE = 1;
private Circle circleRed, circleYellow, circleGreen;
private Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
private int onColor = 0;
@Override
public void start(Stage primaryStage) {
circleRed = new Circle(RADIUS);
circleRed.setFill(Color.WHITE);
circleGreen = new Circle(RADIUS);
circleGreen.setFill(Color.WHITE);
circleYellow = new Circle(RADIUS);
circleYellow.setFill(Color.WHITE);
TilePane light = new TilePane(circleGreen, circleYellow, circleRed);
light.setPadding( new Insets(20, 20, 20, 20) );
Scene scene = new Scene(light, RADIUS*8, RADIUS*3);
primaryStage.setTitle("Traffic Light Simulator");
primaryStage.setScene(scene);
primaryStage.show();
update();
}
private void update() {
PauseTransition pause = new PauseTransition(Duration.seconds(PAUSE));
pause.setOnFinished(event ->{
circleRed.setFill((onColor == 0) ? colors[onColor] :Color.WHITE );
circleYellow.setFill((onColor == 1) ? colors[onColor] :Color.WHITE);
circleGreen.setFill((onColor == 2) ? colors[onColor] :Color.WHITE);
onColor = ((onColor +1) >= colors.length) ? 0 : onColor+1;
pause.play();
});
pause.play();
}
public static void main(String[] args) {
launch(args);
}
}
Edit
If you need to do it with threads, although I think it is not the optimal tool, try this technique:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class TrafficLight extends Application{
private static final double RADIUS = 50;
private Circle circleRed, circleYellow, circleGreen;
private Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
private int onColor = 0;
private static int threadNumber = 0, invokeThreadNumber =0;
private static final Object myLock = new Object();
@Override
public void start(Stage primaryStage) {
circleRed = new Circle(RADIUS);
circleRed.setFill(Color.WHITE);
circleGreen = new Circle(RADIUS);
circleGreen.setFill(Color.WHITE);
circleYellow = new Circle(RADIUS);
circleYellow.setFill(Color.WHITE);
TilePane light = new TilePane(circleGreen, circleYellow, circleRed);
light.setPadding( new Insets(20, 20, 20, 20) );
Scene scene = new Scene(light, RADIUS*8, RADIUS*3);
primaryStage.setTitle("Traffic Light Simulator");
primaryStage.setScene(scene);
primaryStage.show();
control();
}
private void control() {
//invoke 3 synchronized control threads
new Thread( new ColorControl()).start();
new Thread( new ColorControl()).start();
new Thread( new ColorControl()).start();
}
private void update() {
circleRed.setFill((onColor == 0) ? colors[onColor] :Color.WHITE );
circleYellow.setFill((onColor == 1) ? colors[onColor] :Color.WHITE);
circleGreen.setFill((onColor == 2) ? colors[onColor] :Color.WHITE);
onColor = ((onColor +1) >= colors.length) ? 0 : onColor+1;
}
class ColorControl implements Runnable {
private int threadID;
private static final long PAUSE = 1000;
private int MAX_THREADS = 3;
private boolean isStopped = false;
ColorControl() {
threadID = threadNumber ++;
}
void reset() {
threadNumber = 0; invokeThreadNumber =0;
}
@Override
public void run() {
synchronized (myLock) {
while (! isStopped ) {
while (threadID != invokeThreadNumber) {
try {
myLock.wait();
} catch (InterruptedException e) {}
}
//do work here
update();
try {
Thread.sleep(PAUSE);
} catch (InterruptedException ex) { ex.printStackTrace();}
invokeThreadNumber++;
myLock.notifyAll();
if( invokeThreadNumber >= MAX_THREADS ) {
reset();
}
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1