Google App Engine(java)服务类是线程安全的吗? [英] Are Google App Engine (java) service classes Thread-Safe?
问题描述
是否可以从工厂获得对服务的引用,并将其用于处理多个请求?使用以下伪代码可以很好地解释servlet:
SomeServlet ... {
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
doGet(...){
userService.doSomething(...);
}
...
}
可能是不成熟的优化,但我只是想知道为每个请求获得服务的成本是多少。请分享您的洞察力。
解决方案在google group thread http://groups.google.com/group/google-appengine-java/browse_thread/thread/d3f1536084f59c22 ,Ikai Lan (来自Google的GAE团队)表示MemcacheService是线程安全的,但缓存它并不有用,因为每次从工厂获得服务时都只有一个对象分配。
由于所有服务都是以类似的方式获得的,我认为我们可以假设它们都遵循相同的设计并且都是线程安全的。但是由于在每个查询中获取新实例并不昂贵,我不会缓存它们。
Is it okay to obtain a reference to a service from factory once and use it for handling multiple requests? It can be best explained using following pseudo-code for a servlet:
SomeServlet...{
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
doGet(...){
userService.doSomething(...);
}
...
}
It may be a premature optimization but i am just curious to know what is the cost of obtaining service for each request. Please share your insight.
In the google group thread http://groups.google.com/group/google-appengine-java/browse_thread/thread/d3f1536084f59c22, Ikai Lan (from the GAE team at Google) says MemcacheService is thread-safe, but that caching it is not useful, because there is just one object allocation each time you get the service from the factory.
Since all the services are obtained in a similar way, I think we could assume that they all follow the same design and are all thread-safe. But since it isn't costly to get a new instance at each query, I wouldn't cache them.
这篇关于Google App Engine(java)服务类是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!