【发布时间】:2019-12-28 18:39:35
【问题描述】:
我正在学习带进度条的多线程。我在以下简单代码中得到了没有问题的更新:
public class Controller implements Initializable {
@FXML
private Button id_boton1;
@FXML
private Button id_boton2;
@FXML
private ProgressBar id_progressbar1;
@FXML
private ProgressIndicator id_progressindicator1;
@Override
public void initialize(URL location, ResourceBundle resources) {
id_progressbar1.setProgress(0.0);
id_boton1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Thread hiloProgressBar= new Thread(new bg_Thread1());
//hilo.setDaemon(true);
hiloProgressBar.start();
}
});
id_boton2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Thread hiloProgressIndicate = new Thread(new bg_Thread2());
hiloProgressIndicate.start();
}
});
}
class bg_Thread1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
id_progressbar1.setProgress((float)i/99);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
class bg_Thread2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
id_progressindicator1.setProgress((float)i/99);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}}
没什么特别的。 2 个按钮,使用作业和进度条创建线程。
我的问题是关于 CountDownLatch 对该程序的实施。我想等待两个线程完成。现在 UI 不会随着进度条的变化而刷新
public class Controller implements Initializable {
@FXML
private Button id_boton1;
@FXML
private ProgressBar id_progressbar1;
@FXML
private ProgressIndicator id_progressindicator1;
@Override
public void initialize(URL location, ResourceBundle resources) {
CountDownLatch countDownLatch = new CountDownLatch(2);
id_progressbar1.setProgress(0.0);
id_boton1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("iniciando Threads");
Thread hiloProgressBar= new Thread(new bg_Thread1(countDownLatch));
hiloProgressBar.start();
Thread hiloProgressIndicate = new Thread(new bg_Thread2(countDownLatch));
hiloProgressIndicate.start();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("*fin*");
}
});
}
class bg_Thread1 extends Thread{
private CountDownLatch latch;
public bg_Thread1(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
id_progressbar1.setProgress((float)i/99);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
latch.countDown();
}
}
class bg_Thread2 extends Thread{
private CountDownLatch latch;
public bg_Thread2(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
for (int i = 0; i < 40; i++) {
id_progressindicator1.setProgress((float)i/39);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
latch.countDown();
}
}}
现在,一个按钮启动两个线程,当工作完成时progressBar更新一次
【问题讨论】:
-
查看Concurrency in JavaFX。你的问题是你阻塞了 JavaFX 应用程序线程,你不应该这样做。
-
谢谢斯劳。是的,问题在于 JavaFX 应用程序线程。不幸的是,我找不到解决方案。
-
你必须使用CountdownLatch吗?
-
与您的问题无关:请学习 java 命名约定并遵守它们。你快到了,现在用驼峰式替换下划线,它是完美的:)
-
我想使用 CountdownLach 或其他等价物。
标签: java multithreading user-interface javafx countdownlatch