识别 Swing 线程以在 Java 中显示频繁的数据输入

发布于 2024-10-31 13:34:45 字数 925 浏览 0 评论 0原文

对于我当前的应用程序,我正在努力识别应用程序中的 Swing 线程。对于 Swing 线程,我的意思是:

  • 初始线程
  • 事件调度线程
  • 工作线程

我的应用程序:

  • 简单的用户界面,应该显示在套接字上接收到的数据
  • 数据由许多模型类描述
  • 接收到的数据是经过解析的 XML 和模型对象实例化后,
  • 用户界面应该显示接收到的数据,
  • 这些数据更新非常频繁,这意味着 XML 消息很短,但有很多消息
  • 可以将其放入上下文中:我正在编写一个 Java 分析器。

到目前为止,我已经阅读了 Swing 教程,所以这里是我的猜测和问题:

后台任务是服务器套接字,后台任务分别是应用程序接收数据的打开连接的数量。

  1. 任务没有最终结果,所以我猜 SwingWorker 应该只定义临时结果的通用类型?对于每个解析的 XML,我都会调用 publish。但是我如何区分我收到了哪些数据呢?也许 XML 数据仅包含构建 class A 的足够信息,或者数据包含足够的信息来构建 class Aclass B,但是如何将两者合并为一个临时结果?包装类?

  2. process() 方法调用更改以使其对用户界面可见,不是吗?我不明白这是如何运作的。我在哪里启动我的任务?是为了调用 JFrame 构造函数中的 SwingWorker.execute() 吗?

  3. 应该 XML 读取器作为任务,还是应该将处理传入连接的每个线程作为任务?

For my current application, I am struggling with identifying the Swing threads in my application. With Swing threads I mean:

  • Initial threads
  • The event dispatch thread
  • Worker threads

My application:

  • simple user interface which is supposed to display data received on a socket
  • the data is described by many model classes
  • the received data is XML which is parsed and the model objects are instantiated
  • the user interface is supposed to display the received data
  • these data is updated very frequently, which means the XML messages are short, but there are many of them
  • to put it into context: I am programming a Java profiler.

I have read the Swing tutorial so far, so here are my guesses and questions:

The background task is the server socket, respectively the background tasks are the number of opened connections on which the application receives data.

  1. The tasks have no final result, so I guess the SwingWorker<T,S> should only define the generic type for the Interim Result? For every parsed XML I would make a call to publish. But how do I distinguish which data I have received? Maybe the XML data contains only enough information to build a class A or maybe the data contains enough information to build class A and class B, but how do I wrap both into one Interim Result? A wrapper class?

  2. The process() method invokes changes to make it visible to the user interface, doesn't it? I don't see how this works. Where do I launch my tasks? Is it in order to invoke the SwingWorker.execute() in the JFrame constructor?

  3. Should the XML Reader be the Task or should each Thread which handles an incoming connection be the task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-11-07 13:34:45

在您描述的上下文中,我不确定是否会使用 SwingWorker

我的基本想法是:

  • 从您的 main() 开始,启动多个线程来服务套接字(标准 Thread API),
  • 当一个这样的套接字线程获取一些输入时,它会解析 XML立即(正如您所描述的,解析应该非常快,因此我认为您不需要为此启动一个新线程)
  • 一旦解析了 XML,套接字线程就会创建一些“结果对象< /strong>”要显示,在 final 变量中声明此对象,然后调用 SwingUtilities.invokeLater() 在 UI 中显示此结果

我使用过的另一个替代方案过去成功的方法是使用 EventBus 来负责调用 EDT 中的 UI 更新方法,您的套接字线程会将“结果对象”发送到该 EventBus。

关于 SwingWorker 的使用,我想说主要用途是当最终用户启动一个操作(例如通过单击按钮或菜单项)并且该操作很长并且应该在后台处理时,后台然后处理方法必须将信息反馈给 UI。

In the context you describe, I am not sure I would use SwingWorker.

My basic idea would be:

  • from your main(), start several threads for serving sockets (standard Thread API)
  • when one such socket thread gets some input, it parses the XML right away (as you describe it, parsing should be very fast, hence I don't think you would need to start a new thread just for that)
  • once the XML is parsed, the socket thread creates some "result object" to be displayed, declare this object in a final variable, and call SwingUtilities.invokeLater() to display this result in your UI

Another alternative that I have used successfully in the past would be to use an EventBus that would take care of callingthe UI update method in the EDT, your socket threads would send the "result object" to that EventBus.

About SwingWorker use, I would say the main use is when the end user starts an action (e.g. by clicking a button or a menu item) and this action is long and should be processed in background, the background processing method would then have to feed back information to the UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文