JavaFx2或ScalaFx + Akka [英] JavaFx2 or ScalaFx + Akka

查看:187
本文介绍了JavaFx2或ScalaFx + Akka的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaFX / ScalaFX应用程序中运行Akka actor?

How to run Akka actors in a JavaFX/ScalaFX Application ?

(这是基于第一个答案的问题更新)

(This is an update of question based on the first answers)

解决方案是否共享相同的执行上下文?是否有基于JavaFx ExecutorService的Actors调度程序? (运行UI操作代码的那个)

Is the solution to share the same execution context? Meaning having the Actors dispatchers based on the JavaFx ExecutorService ? (The one with which on which it runs UI manipulation code)

这是否意味着一个代理将代表UI并能够操作它?我的意思是因为如果有两个演员在UI ExecutorService上,下面建议,这是不是意味着在代理(对象是UI)之间共享状态?

Does that mean one agent would represent the UI and be able to manipulate it ? I mean because suggested below if a couple of actor are on the UI ExecutorService, doesn't that mean share a state between agent (the object being the UI)?

可以2个演员在进行不同的执行者服务时进行交流?我问这个是因为根据下面的建议,一些代理将在UI Executor服务上,而其他代理不在。

Can 2 actors communicate while being on different executor services? I'm asking this because from what is suggested below, some agent would be on the UI Executor Service while other not.

最后,为什么使用akka,因为它在Executor上下文不同并使用Platform.runLater,可能会对UI的性能产生一些影响。我在同一个应用程序中提出了多个执行程序服务的问题:这是不是很糟糕?

Finally, why using akka as is, with its on Executor context different and using Platform.runLater, might have some consequence on the performance of the UI. I this pose the question of multiple executor service on the same application: Is that bad?

推荐答案


  • 期货

  • 使用scala Futures和单线程工具包(如JavaFX)的最佳方法是定义一个允许你的执行器在UI Toolkit的线程上执行future或actors。

    The best way to use scala Futures together with a single-threaded toolkit such as JavaFX would be to define an executor that allows you to execute futures or actors on the thread of the UI Toolkit.

    Swing存在同样的问题,这也需要在swing线程上进行更新。 Viktor Klang提出了以下解决方案: Swing Execution Context 。这里是为JavaFX翻译的:

    The same problem exists for Swing, which also requires updates to happen on the swing thread. Viktor Klang came up with the following solution Swing Execution Context. Here it is translated for JavaFX:

    import akka.dispatch.ExecutionContext
    import javafx.application.Platform
    import java.util.concurrent.Executor
    
    //
    object JavaFXExecutionContext {
      implicit val javaFxExecutionContext: ExecutionContext = ExecutionContext.fromExecutor(new Executor {
      def execute(command: Runnable): Unit = Platform.runLater(command)
      })
    }
    

    您可以这样使用它:

    // import the default ec
    import scala.concurrent.ExecutionContext.Implicits.global
    // define the JavaFX ec so we can use it explicitly
    val fxec = JavaFXExecutionContext.javaFxExecutionContext
    future {
      // some asynchronous computation, running on the default
      // ForkJoin ExecutionContext because no ec is passed
      // explicitly
    }.map(result => {
      // update JavaFX components from result
      // This will run in the JavaFX thread.
      // Check Platform.isFxApplicationThread() to be sure!
    })(fxec)
    

    期货的管道可能非常复杂,只要与JavaFX组件交互的步骤都在JavaFX ExecutionContext上运行。

    The pipeline of futures can be very complex, as long as the step(s) that interact with JavaFX components are all running on the JavaFX ExecutionContext.

    注意:是否使ForkJoin ec成为默认值并通过,由您决定JavaFX ec明确地反之亦然。最好使JavaFX ec成为默认值以防止错误,并使用ForkJoin ec标记可以异步运行的部分。

    Note: it is up to you whether you make the ForkJoin ec the default and pass the JavaFX ec explicitly or vice versa. It might be a good idea to make the JavaFX ec the default to prevent errors and mark the parts that can run asynchronously explicitly with the ForkJoin ec.


    • Actors

    为了将基于Actor的系统与单线程UI工具包集成,还有一个解决方案。请参阅 Swing Actors 。你所要做的就是替换

    For integrating an Actor based system with a single-threaded UI toolkit there is also a solution. See Swing Actors. All you have to do is to replace the

    SwingUtilities.invokeLater(command)
    

    with

    Platform.runLater(command)
    

    你很高兴!


    • 何时使用

    如果你有一个很棒的UI应用程序而只想分离一些异步操作(加载文件或进行一些计算),基于期货的方法可能更可取。但是请注意不要在默认执行上下文中异步运行的期货中与JavaFX组件进行任何交互(既不读也不写)。

    If you have a big UI application and just want to spin off some asynchronous operations (loading a file or doing some computation), the futures-based approach is probably preferable. But be careful not to do any interaction (neither read nor write) with JavaFX components in the futures that run asynchronously on the default execution context.

    如果您有一个基于actor的大型系统并且只想将UI附加到某些部分,那么在JavaFX线程上运行一些actor可能更可取。 YMMV。

    If you have a large actor based system and just want to attach an UI to some parts, having a few actors that run on the JavaFX thread is probably preferable. YMMV.

    这篇关于JavaFx2或ScalaFx + Akka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆