如何配置 Unity 进行类型/模块的异步初始化
如何设置 Unity,以便类可以异步初始化,而不会阻止其他模块的加载(仅阻止明确需要异步类型实例的其他类型)?我正在考虑的类是引用数据缓存,它从数据库中提取常用数据的快照,并且我需要在让任何其他模块访问它之前完成预缓存(如果请求在我的类中被阻止)将快速阻止主线程并阻止所有其他模块初始化)。这变得更加重要,因为我有多个这样的引用数据类
作为一个例子,假设我有一个这样的类:
public class ProductCache{
public ProductCache(){}
public Initialize(){
// a very slow DB call to fetch frequently used products
Thread.Sleep(30*1000);
}
public Product FindProduct(string productDescription){
/* check cache, if not there try the db */
}
}
如果我从构造函数调用Initialize,我将阻塞调用它的线程(从Unity)30秒,阻止我并行创建其他(类似)类。如果我只是将任务放在线程池上,Unity 最终将到达需要我的产品缓存的另一个类正在执行其初始化代码,然后它会访问尚未完全初始化的数据结构(在这种情况下,会导致在缓存未命中和调用数据库来获取特定产品的情况下,30 秒内可能会有很多此类请求)
谢谢 奥斯卡
How do I set up Unity such that a class can intialize asynchronously without blocking the load of other modules (only blocking other types that explicitly need an instance of the asynch type)? The kind of classes I'm thinking of are reference data caches that pull in a snapshot of frequently used data from the database, and I need to finish precaching before I let any other modules access it (if the requests are blocked within my class I will quickly hold up the main thread and block all other modules from initializing). This becomes more important as I have multiple such reference data classes
As an example, say I have a class like this:
public class ProductCache{
public ProductCache(){}
public Initialize(){
// a very slow DB call to fetch frequently used products
Thread.Sleep(30*1000);
}
public Product FindProduct(string productDescription){
/* check cache, if not there try the db */
}
}
If I call Initialize from the constructor I will block the thread that invokes it (from Unity) for 30 seconds, preventing me from creating other (similar) classes in parallel. If I simply put the task on the threadpool Unity will eventually get to the point where another class that needs my product cache is executing its initialization code, and it is then accessing datastructures that aren't fully initialized yet (in this case it would result in a cache miss and a call to the db to fetch the particular product, and there might be many such requests in 30 seconds)
thanks
Oskar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要列出正在运行的任务,并行执行它们,并使用 Task.WaitAll() 等待它们完成后再继续。
在 .Net 4 中,这应该可行,并且错误处理变得容易:
在老式 .Net 中,尝试这个。我假设您为每个可初始化对象使用一个接口,并且我省略了错误处理:
You need to make a list of the running tasks, execute them in parallel, and use Task.WaitAll() to wait for them to complete before continuing.
In .Net 4, this should work, and error handling is made easy:
In old-school .Net, try this. I've assumed you're using an interface for each initializable object, and I've left out error handling: