使用多线程在特定时间内更改javafx圈子的颜色 [英] Change javafx circles color in certain time using multithreads
本文介绍了使用多线程在特定时间内更改javafx圈子的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用javafx创建交通信号灯模拟器,它使用多线程概念每2秒更改一次颜色(第一个红灯闪烁并保持2秒).我有我的代码如下.但是它没有按预期工作.它只会闪烁所有指示灯,直到有人可以帮我弄清楚我出了什么问题吗?谢谢
I am creating traffic light simulator with javafx that changes colour in every 2 seconds (first red light blinks and remain for 2 seconds) using the concept of multithreading. I have my code as given below. But it doesn't work as expected. It just blinks all light and sto Can somebody help me figure it out where I went wrong? Thanks
public void startThreads() throws InterruptedException {
Runnable taskOne = new Runnable(){
@Override
public void run(){
try {
Platform.runLater(new Runnable() {
@Override
public void run() {
circle.setFill(Color.RED);
circle1.setFill(Color.GREY);
circle2.setFill(Color.GREY);
}
});
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable taskTwo ......
Runnable taskThree .....
Thread thread1 = new Thread(taskOne);
Thread thread2 = new Thread(taskTwo);
Thread thread3 = new Thread(taskThree);
//Start thread 1
thread1.start();
thread1.join();
//start thread 2
thread2.start();
thread2.join();
//Start thread 3
thread3.start();
thread3.join();
}
推荐答案
查看全文