I have few line of cords that need to be executed upon the running Thread
completion. So i used thread.join()
keyword right before the cords that i want to execute later.
When i run the program with join()
keywords, My App interface get stuck until thread execution completed. How do i overcome this issue..?
Actually what my program doing is, once i press button it will execute method call Pass_data_from_java_to_report();
and while it's running, a separate view or Stage
will appear to notify the user that program is still working. once above method finish execution waiting Stage
will be closed by calling stage.close()
. Those operations are working fine but the freezing with join keyword.
This is my first approach.
if(event.getSource() == btnAdd){
Stage stage = Waiting(); // Returns Waiting Message
Task <Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Pass_data_from_java_to_report(); // My Method
return null;
}
};
// Displaying Waiting Message
task.setOnRunning(eve -> {
stage.show();
});
// Closing Waiting Message
task.setOnSucceeded(e -> {
stage.close();
});
// Thread for executing task
Thread t1 = new Thread(() -> {
task.run();
});
t1.start();
t1.join(); // if i remove this buddy, there is no freezing
// below from here i want to execute upon thread completion
System.out.println("Finish ");
}
Second Approach
if(event.getSource() == btnAdd){
Stage stage = Waiting();
Thread t1 = new Thread(() -> {
Platform.runLater(() -> {
stage.show();
});
});
Thread t2 = new Thread(() -> {
Pass_data_from_java_to_report(); // My Method
Platform.runLater(() -> {
// Closing the Stage running on another thread upon Above Method Completion
stage.close();
});
});
t1.start();
t2.start();
t1.join(); // These are the Problems
t2.join(); // this one too
System.out.println("Finish ");
}
Here is the Method
public void Pass_query_to_report(){
try {
// Since method is not time consuming,
// i added 3 sec sleep before the Execution
Thread.sleep(3000);
/*
Some cord
*/
} catch (Exception e) {
e.printStackTrace();
}
}
(Note - i also tried to use CountDownLatch
but no luck)
Thread.join()
blocks the current thread until the specified thread completes. Since you're calling it on the FX Application Thread, which is responsible for rendering the UI and processing user events, it blocks that thread, making the UI unresponsive.
Just move the code that needs to be executed after the task completes to the onSucceeded
handler:
if(event.getSource() == btnAdd){
Stage stage = Waiting(); // Returns Waiting Message
Task <Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Pass_data_from_java_to_report(); // My Method
return null;
}
};
// Displaying Waiting Message
task.setOnRunning(eve -> {
stage.show();
});
// Closing Waiting Message
task.setOnSucceeded(e -> {
stage.close();
// below from here i want to execute upon thread completion
System.out.println("Finish ");
});
// Thread for executing task
Thread t1 = new Thread(task);
t1.start();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.