I'm learning multithreads with progressbar. I have got update without problems in the following simple code:
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);
}
}
}}
Nothing special. 2 buttons that create thread with a job and progressBar.
My question is about the implementation of CountDownLatch to this program. I want to wait for the two threads to finish. Now the UI does not refresh with the progressbar changes
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();
}
}}
Now, One button start two threads, when the work is finish the progressBar is updated one time