识别 Swing 线程以在 Java 中显示频繁的数据输入
对于我当前的应用程序,我正在努力识别应用程序中的 Swing 线程。对于 Swing 线程,我的意思是:
- 初始线程
- 事件调度线程
- 工作线程
我的应用程序:
- 简单的用户界面,应该显示在套接字上接收到的数据
- 数据由许多模型类描述
- 接收到的数据是经过解析的 XML 和模型对象实例化后,
- 用户界面应该显示接收到的数据,
- 这些数据更新非常频繁,这意味着 XML 消息很短,但有很多消息
- 可以将其放入上下文中:我正在编写一个 Java 分析器。
到目前为止,我已经阅读了 Swing 教程,所以这里是我的猜测和问题:
后台任务是服务器套接字,后台任务分别是应用程序接收数据的打开连接的数量。
任务没有最终结果,所以我猜
SwingWorker
应该只定义临时结果的通用类型?对于每个解析的 XML,我都会调用publish
。但是我如何区分我收到了哪些数据呢?也许 XML 数据仅包含构建class A
的足够信息,或者数据包含足够的信息来构建class A
和class B
,但是如何将两者合并为一个临时结果?包装类?process()
方法调用更改以使其对用户界面可见,不是吗?我不明白这是如何运作的。我在哪里启动我的任务?是为了调用JFrame
构造函数中的SwingWorker.execute()
吗?应该 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.
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 topublish
. But how do I distinguish which data I have received? Maybe the XML data contains only enough information to build aclass A
or maybe the data contains enough information to buildclass A
andclass B
, but how do I wrap both into one Interim Result? A wrapper class?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 theSwingWorker.execute()
in theJFrame
constructor?Should the XML Reader be the Task or should each Thread which handles an incoming connection be the task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您描述的上下文中,我不确定是否会使用
SwingWorker
。我的基本想法是:
main()
开始,启动多个线程来服务套接字(标准Thread
API),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:
main()
, start several threads for serving sockets (standardThread
API)final
variable, and callSwingUtilities.invokeLater()
to display this result in your UIAnother 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.