IOC 应用程序中多线程的良好模式是什么(温莎)
我正在使用 IOC 编写一个简单的照片下载应用程序。有一个相当简单的步骤列表。获取要下载的照片列表。检查这个,检查那个,然后下载。在某些情况下,下载可以通过 FTP 完成,在其他情况下可以通过 http 完成,在其他情况下可以通过 Web 服务完成。
由于我是 IOC 领域的新手,我想知道什么是使用 IOC 最佳实践/设计模式来启动 10 个“下载线程”的好模式。
我正在使用 Castle Windsor,但可以轻松切换到 StructureMap 或 Spring,因为我处于该过程的早期阶段。
编辑:要明确的是,我了解如何创建 IPhotoDownloader 接口,然后创建 3 个具体的 photodownloader 类,我不清楚的是如何控制线程以及如何知道所有线程上的所有操作何时完成并继续。
针对评论的第二次编辑: 据我了解,直接引用 IOC 应用程序中的具体类是很糟糕的,因此,如果我想使用 ThreadPool.QueueUserWorkItem 对下载器进行多线程处理,我该怎么做。我是否可以简单地使用容器来解析线程启动方法中每个线程上所需的具体类?喜欢:
void StartThead(){
PhotoRetriever retriever = container.Resolve<PhotoRetriever>();
}
I'm writing a simple photo downloader application using IOC. There is a fairly straightforward list of steps. Get list of photos to download. Check this, Check that, then download. The download can be accomplished via FTP in some cases, and via http in others, and via a web service in others.
What I'm wondering, as I'm new to IOC land, is what is a good pattern to use to spin up 10 'download threads' using IOC best practices/design patterns.
I'm using Castle Windsor but could easily switch to StructureMap, or Spring, as I'm early in the process.
EDIT: To be clear, I understand how to create an IPhotoDownloader interface and then 3 concrete photodownloader classes, what I'm not clear on is how to control the threading and how to know when everything is done on all the threads and continue.
Second Edit in response to comments:
As I understand it, it would be bad to directly reference a concrete class in an IOC application so if I wanted to, for instance, use ThreadPool.QueueUserWorkItem to multithread my downloader, how would I do that. Would I simply use the container to resolve the concrete class I want on each thread inside my thread start method? like:
void StartThead(){
PhotoRetriever retriever = container.Resolve<PhotoRetriever>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Krysztof 让我开始走上正确的道路,基本上我在做这样的事情时看到了一些奇怪的行为:
while (fileUri != null)
{
我看到的行为是因为我不知道生命周期属性(默认的 Singleton),而实际上我需要在这个实例中使用 PerThread。
Krysztof got me started on the right path, basically I was seeing some weird behavior when doing something like this:
while (fileUri != null)
{
The behavior I was seeing was because I was not aware of the lifetime properties, default Singleton, when in fact I needed to use PerThread in this instance.