Mam klasę implementującą interfejs runnable
:
public class ThreadInterfaceImplementation implements Runnable {
private volatile boolean running = true;
public void terminate() {
running = false;
}
@Override
public void run() {
while(running)
{
System.out.println("I'm still running");
}
}
}
Moja klasa główna wygląda tak:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("layout").load(), 630, 400);
stage.setScene(scene);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml).load());
}
private static FXMLLoader loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader;
}
public static void main(String[] args) {
launch();
}
}
W klasie którą wywołuje klasa App
po przez scene = new Scene(loadFXML("layout").load(), 630, 400)
mam powiedzmy, że coś takiego:
public class Controller implements Initializable {
private ThreadInterfaceImplementation runnable = null;
private Thread thread = null;
@Override
public void initialize (URL url, ResourceBundle resourceBundle) {
runnable = new ThreadInterfaceImplementation();
thread = new Thread(runnable);
thread.start();
}
}
I teraz chcę zamknąć wątek przy wyłączaniu aplikacji krzyżykiem. Jak mam to zrobić?