Javafx 线程加入交通灯 UI。如何同步线程?
Javafx thread join on traffic Light UI. How do I synchronize threads ?
我有一个模拟交通灯的 javafx UI。我为每个灯设置了三个线程,它们分别点亮各自的灯,同时关闭其他灯。我启动线程并使用 join() 以便每个线程都可以在另一个线程启动之前点亮各自的灯。
当我使用 Thread.join() 时,UI 冻结但我知道线程 运行 正确,因为我放入线程中的测试字符串执行并等待在另一个执行之前的适当时间。
代码如下-
主要方法
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){
}
}
}
线程 类 -
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) {
}
}
}
我该如何解决这个问题?我被告知可以使用 Platform.runlater 但不确定如何使用它。我需要为此任务使用多线程。
这里是如何使用 JavaFx PauseTransition 更新 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);
}
}
编辑
如果你需要用线程来做,尽管我认为它不是最佳工具,试试这个技术:
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);
}
}
我有一个模拟交通灯的 javafx UI。我为每个灯设置了三个线程,它们分别点亮各自的灯,同时关闭其他灯。我启动线程并使用 join() 以便每个线程都可以在另一个线程启动之前点亮各自的灯。
当我使用 Thread.join() 时,UI 冻结但我知道线程 运行 正确,因为我放入线程中的测试字符串执行并等待在另一个执行之前的适当时间。
代码如下-
主要方法
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){
}
}
}
线程 类 -
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) {
}
}
}
我该如何解决这个问题?我被告知可以使用 Platform.runlater 但不确定如何使用它。我需要为此任务使用多线程。
这里是如何使用 JavaFx PauseTransition 更新 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);
}
}
编辑
如果你需要用线程来做,尽管我认为它不是最佳工具,试试这个技术:
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);
}
}