关于管道和过滤器实施的一些问题
我将开发一个名为 ExtractInfoFromUrl
的组件。该组件有一个名为 addUrl(url)
的方法,该方法接受 url 并打开给定的 url 并从相应页面提取信息,完成后引发一个事件。该组件内部由管道和过滤器组成。
我有 3 个问题:
我想知道什么会更好——让每个
Filter
都有一个Thread
(也就是说,在 Java 中,继承自Thread
) 或让Pipe
有Threads
?很明显,我的过滤器或管道必须带有螺纹。但是我是否也必须为我的组件本身使用线程?我需要一个控制其他线程的线程,并且我相信主程序的线程不适合该任务,但我不太确定为什么。
是否有任何其他类型的 PipedReaders/Writers Java 实现允许我处理其他类型的数据而不是 char/int ?我想说,这对我来说有点太低了。例如,如果有其他允许使用字符串的方法,那就更好了。
谢谢
I am going to develop a component called ExtractInfoFromUrl
. This component has a method called addUrl(url)
that accepts urls and will open the given url and extract info from the corresponding page, raising an event when done. Internally, the component is composed of pipes and filters.
I have 3 questions:
I'd like to know what would be better -- to have each
Filter
have aThread
(that is, in Java, inherit fromThread
) or have thePipe
s haveThreads
?It is obvious that I will have to have my filters or my pipes with threads. But won't I have, too, to use a Thread for my component itself? I need a thread that controls the other ones, and I believe the main program's thread is not suited for the task, but I am not too sure why.
Is there any other kind of Java implementation for PipedReaders/Writers that allows me to handle other kinds of data instead of char/int ? That is a bit too low level for me, I'd say. If there were some other that'd allow strings instead, for example, it'd be preferable.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一方面,通常最好实现 Runnable 而不是扩展 Thread,但无论如何,我不认为您希望让过滤器或管道扩展 Thread 或实现 Runnable,而是让每个管道都用于一个新的线程。
“组件”是什么意思?你的意思是可视化GUI组件吗?或者不同的东西?
我已将 PipedWriter 包装在 PrintWriter 中
For one, it's usually preferable to implement Runnable rather than extend Thread, but regardless, I don't think that you'll want to have your Filters or Pipes extend Thread or implement Runnable, but rather have each pipe be used in a new Thread.
What do you mean by "component"? Do you mean a visualized GUI component? Or something different?
I've wrapped PipedWriters in PrintWriter
从概念上讲,过滤器代表过滤器和管道架构中的计算,因此我认为线程与过滤器关联更有意义(如果完全使用线程)。管道是模式的“通信”部分。
同样,从概念上讲,如果从检索到的页面中提取的信息不依赖于任何其他数据,则仅在多处理器情况下为它们使用线程才有意义。我也不确定为什么需要主线程 - 也许你可以详细说明。
为什么不考虑消息队列而不是使用低级读取器/写入器呢?
Conceptually, filters represent the computations in a filter-and-pipe architecture, so I would think it makes more sense for the threads to be associated with filters (if threads are used at all). Pipes are the "communication" portion of the pattern.
Again, conceptually, if the information extracted from a retrieved page does not depend on any other data, then using threads for them makes sense only in a multiprocessor case. I'm also not sure why there is a need for a master thread - perhaps you can elaborate.
Instead of using low-level Reader/Writers, why not consider message queues?