gpt4 book ai didi

java - 明确新线程启动和停止的时间和地点

转载 作者:行者123 更新时间:2023-12-02 02:20:03 28 4
gpt4 key购买 nike

我按照在线教程为我的 JavaFX 应用程序构建了启动屏幕。这是 SplashController 文件:

package splash;

import java.io.IOException;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
* SplashController controls the Splash.FXML file. This is the initial software display. After a
* designated amount of time, the MainMenu.FXML is run.
*
*/
public class SplashController {

@FXML
private StackPane splashRoot;
@FXML
private ImageView splashImage;

/**
* Called after all @FXML annotated members have been injected. Starts a new thread "SplashScreen()".
*/
@FXML void initialize() {

new SplashScreen().start(); //Start SplashScreen thread.

}

/**
* Inner class that extends thread. Displays the splash screen for a designated amount of time then loads
* MainMenu.FXML, sets a new stage, and displays the main menu. Hides the splash screen.
*
*/
class SplashScreen extends Thread {

@Override
public void run() {

try {

splashImage.fitWidthProperty().bind( splashRoot.widthProperty() ); // binds the image to the rootPane width.
Thread.sleep( 3000 ); // puts the thread (SplashScreen) to sleep in order to allow the splash to display long enough.

Platform.runLater( new Runnable() { // Forces the main menu scene to load onto the SplashScreen thread when it is available.

@Override
public void run() {

FXMLLoader loader = new FXMLLoader( getClass().getResource( "../mainMenu/MainMenu.FXML" ) ); // Places MainMenu.FXML into the loader.
Stage stage = new Stage(); // Creates a new stage.

try {

stage.setScene( new Scene( loader.load() ) ); // Sets the scene using the root node of the loaded FXML document.

} catch ( IOException e ) {

e.printStackTrace();

}

stage.setResizable(false); // Prevents user from resizing the window.
stage.setTitle( "Test" ); // Sets the stage title.
stage.show(); // Displays the stage.
splashRoot.getScene().getWindow().hide(); // Hides the splash screen and presumably ends the SplashScreen thread.

}

});

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}
}

我试图注释我对线程如何在这里工作的理解,并希望您能为我澄清以下内容是否正确或回答问题:

1) 如果我永远不会创建任何扩展 thread 的新类或创建任何新的 Runnable() 对象,我可以假设我的 JavaFX 项目中的所有内容都是在单个 JavaFX 应用程序线程上运行;

2) 当我创建内部类 SplashScreen extends Thread 并对其调用 start() 时,我是否创建了第二个线程?因此,我有两个线程 - 常规 JavaFX 应用程序线程和 SplashScreen 线程;

3) 当我调用 Platform.runLater( new Runnable() { .... } ) 时,它是否会在 JavaFX 应用程序线程(SplashScreen)中设置新阶段、FXML 和 Controller 线程,还是新的第三个线程?

4)当我调用splashRoot.getScene().getWindow().hide()时,是否会结束SplashScreen线程?或者该线程是否继续执行由 Platform.runLater() 调用的 new Runnable()

最佳答案

1) If I was to never create any new classes that extend thread or create any new Runnable() objects, I could assume that everything in my JavaFX project is running on a single JavaFX application thread;

只要您不调用任何执行此操作的内容(例如从其他 API),情况就是如此。

2) When I create the inner class SplashScreen extends Thread and call start() on it, am I creating a second thread? So I therefore have two threads - the regular JavaFX application thread and the SplashScreen thread;

是的。请注意,SplashScreen 在需要处理 UI 内容时使用 Platform.runLater()

3) When I then call Platform.runLater( new Runnable() { .... } ) does it set that new stage, FXML, and controller in the JavaFX application thread, the SplashScreen thread, or yet a new third thread?

调用Platform.runLater()的代码位于第二个线程上。 Platform.runLater() 中的 Runnable 对象是一个放入 JavaFX 应用程序线程的“队列”中的对象,该线程将在完成其正在执行的操作后运行该对象或需要做的事情(即渲染 UI 等)。

4) When I call splashRoot.getScene().getWindow().hide(), does that end the SplashScreen thread? Or is that thread continuing with the new Runnable() that was called by Platform.runLater()?

它只是关闭承载启动画面的窗口。 线程结束,因为它是线程必须执行的 Runnable 中的最后一行代码 。线程结束是因为将 Runnable 放入 JavaFX 应用程序线程队列后没有其他事情可做。

摘要

  1. 您创建了一个显示启动屏幕的窗口。
  2. 在 Controller 的构造函数中,您创建了另一个线程。
  3. 在该线程中,您让它(新线程) hibernate 3000 毫秒。
  4. 唤醒后,您将返回到 JavaFX 应用程序线程,并在其中加载新内容并打开新窗口,然后关闭启动屏幕。

关于java - 明确新线程启动和停止的时间和地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48634908/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com