尝试从java中的不同线程调用其他方法

尝试从java中的不同线程调用其他方法,java,multithreading,javafx,concurrency,Java,Multithreading,Javafx,Concurrency,我的javaFX应用程序存在性能问题,因此我尝试将一些使用大量计算资源的方法调用到子线程。我尝试过这样做,但没有成功。下面的代码显示了我所做的工作。我哪里出错了。任何帮助都将不胜感激 这是最初创建所有方法的类 public class PrimayModel { // instance variables - replace the example below with your own private static MainController controller = ne

我的javaFX应用程序存在性能问题,因此我尝试将一些使用大量计算资源的方法调用到子线程。我尝试过这样做,但没有成功。下面的代码显示了我所做的工作。我哪里出错了。任何帮助都将不胜感激

这是最初创建所有方法的类

public class PrimayModel 
{
    // instance variables - replace the example below with your own
    private static MainController controller = new MainController();
    
    /**
     * Constructor for objects of class MainModel
     */
    public MainModel()
    {
        
    }

    
    public void doWork()
    {
        //some code
    }
这是一些方法现在所在的新方法。这些方法应该在子线程中执行

public class SeconaryModel implements Runnable
{
    // instance variables - replace the example below with your own
    private static MainController controller = new MainController();    

    /**
     * Constructor for objects of class SecondaryThread
     */
    public void run()
    {
        doSomething();
    }
    
    public int doSomething()
    {
        //some code
    }
这就是在JavaFX控制器的populateWindow()方法中调用上述两个类的方法的地方

private void populateWindow() 
    {
        SecondaryThread secondaryModel = new SecondaryModel();
        Thread thread = new Thread(new SecondaryModel());
        thread.start();

        String temp = String.valueOf(secondaryModel.doSomething());
        String temp2 = String.valueOf(secondaryModel.doWork());

        //set info labels.
        topLeftInfoLabel.setText(temp);
        topRightInfoLabel.setText(temp2);
        
    }

我对JavaFX了解不多,但如果我认为“Platform.runLater”是这样的话,那么使用下面的代码应该可以

// Initialize Secondary Model
SecondaryModel secondaryModel = new SecondaryModel();

new Thread(() -> {

    // Do what you would do inside your "run" method of your SeconaryModel
    // This will run on the thread

    // You save your temp values for later use on the main thread
    String temp = String.valueOf(secondaryModel.doSomething());
    String temp2 = String.valueOf(secondaryModel.doWork());

    // Call Platform.runLater after you are done processing stuff on the thread
    Platform.runLater(() -> {

        // This will now run on the main thread again.
        // Meaning you will do the following in here:

        topLeftInfoLabel.setText(temp);
        topRightInfoLabel.setText(temp2);

    });

}).start();
或者,如果您没有Java 1.8+版本,请使用:

// Initialize Secondary Model
SecondaryModel secondaryModel = new SecondaryModel();

new Thread(new Runnable() {
    @Override
    public void run() {
        
        // Do what you would do inside your "run" method of your SeconaryModel
        // This will run on the thread

        // You save your temp values for later use on the main thread
        String temp = String.valueOf(secondaryModel.doSomething());
        String temp2 = String.valueOf(secondaryModel.doWork());

        // Call Platform.runLater after you are done processing stuff on the thread
        Platform.runLater(new Runnable() {
            @Override
            public void run() {

                // This will now run on the main thread again.
                // Meaning you will do the following in here:

                topLeftInfoLabel.setText(temp);
                topRightInfoLabel.setText(temp2);
                    
            }
        });
            
    }
}).start();

内容:SecondaryThread secondaryModel=新的secondaryModel();Secondary Model a Secondary thread是如何工作的?这不是它的工作方式。线程完成后,您需要填充窗口,这意味着您需要在线程上处理数据后将其“发送”回主线程,然后将其设置到屏幕上的组件中。您可能只能从主线程更新JavaFX组件。我下面的回答对您有帮助吗?请代码片段看起来很奇怪:字段的静态作用域几乎总是错误的,尤其是在扩展类中隐藏字段时(而不是以某种方式传递参数),并且确保遵守ui线程规则(使用fx并发支持vs普通线程)
Platform.runLater()
显示一个未声明的变量错误。@Leonard我以为您在使用JavaFX@Leonard您可以用任何其他允许您在主线程上运行代码的东西替换Platform.runLater。您是否安装了Java 1.8+,因为如果没有,您需要尝试我发布的第二个代码。还要确保平台已正确导入。也许您已经安装了Java 1.8+错的