【问题标题】:Easy way to call method in new thread在新线程中调用方法的简单方法
【发布时间】:2013-03-29 19:47:40
【问题描述】:

我正在编写小应用程序,现在我发现了一个问题。 我需要调用一个(以后可能是两个)方法(这个方法加载一些东西并返回结果)而不滞后于应用程序的窗口。

我找到了 ExecutorCallable 这样的类,但我不明白如何使用这些类。

您能否发布任何对我有帮助的解决方案?

感谢所有建议。

编辑:方法必须返回结果。这个结果取决于参数。 像这样的:

public static HtmlPage getPage(String page) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        return webClient.getPage(page);
}

此方法的工作时间约为 8-10 秒。执行此方法后,可以停止线程。但我需要每 2 分钟调用一次方法。

编辑:我用这个编辑了代码:

public static HtmlPage getPage(final String page) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    Thread thread = new Thread() {
        public void run() {
            try {
                loadedPage = webClient.getPage(page);
            } catch (FailingHttpStatusCodeException | IOException e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    try {
        return loadedPage;
    } catch (Exception e) {
        return null;
    }

}

使用此代码,我再次遇到错误(即使我将 return null 放在 catch 块之外)。

【问题讨论】:

  • ExecutorExecutorService 在 javadoc 中有示例。 a topic 在官方教程的 concurrency 线程上。这就是我开始的地方。如果您不理解这些材料中的具体内容,或者您​​一直无法使用其中的信息,您应该提出一个新问题。
  • 我看到了例子,但我无法实现我想要的。
  • 认为您正在寻找 ExecutorService.submit()Callable<HtmlPage>,因此请关注这些和相关类/方法的文档。 submit() 应该返回一个 Future<HtmlPage>,当结果可用时,它会让你做一些事情(比如更新 UI)。
  • 我会看到的,谢谢。

标签: java multithreading javafx ui-thread


【解决方案1】:

从 Java 8 开始,您可以使用更短的形式:

new Thread(() -> {
    // Insert some method call here.
}).start();

更新: 另外,您可以使用方法参考:

class Example {

    public static void main(String[] args){
        new Thread(Example::someMethod).start();
    }

    public static void someMethod(){
        // Insert some code here
    }

}

当您的参数列表与所需的@FunctionalInterface 相同时,您可以使用它,例如RunnableCallable

更新 2: 我强烈建议使用java.util.concurrent.Executors#newSingleThreadExecutor() 来执行即发即弃的任务。

例子:

Executors
    .newSingleThreadExecutor()
    .submit(Example::someMethod);

查看更多:Platform.runLater and Task in JavaFXMethod References

【讨论】:

    【解决方案2】:

    首先,我建议您查看Java Thread Documentation

    使用线程,您可以传入称为Runnable 的接口类型。可以在here 找到文档。可运行对象是具有run 方法的对象。当您启动一个线程时,它将调用此可运行对象的run 方法中的任何代码。例如:

    Thread t = new Thread(new Runnable() {
             @Override
             public void run() {
                  // Insert some method call here.
             }
    });
    

    现在,这意味着当您调用t.start() 时,它将运行您需要的任何代码,而不会滞后于主线程。这称为Asynchronous 方法调用,这意味着它与您打开的任何其他线程并行运行,例如您的main 线程。 :)

    【讨论】:

    • 我尝试了类似的方法,但没有成功(更多信息在下一个答案中)
    • 如何将参数传递给调用方法
    【解决方案3】:

    在 Java 8 中,如果不需要参数,您可以使用:

    new Thread(MyClass::doWork).start();
    

    或者在参数的情况下:

    new Thread(() -> doWork(someParam))
    

    【讨论】:

    • 那是相对的。参数只是隐藏的,它取决于接口的输入参数。当你写new Thread(MyClass::doWork).start(); 时,你也可以写new Thread(() ->doWork()) 因为Runabble 没有输入参数。但是,如果您需要来自闭包之外的变量的输入,那么可以,您可以采用第二种方式。
    猜你喜欢
    • 2011-12-22
    • 2012-01-22
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2015-10-15
    相关资源
    最近更新 更多