我从"java concurrency pratique"一书中得到了一个例子,他说易失性和不可变的持有者对象给出了线程安全性.但我不明白这本书给出的例子.
代码如下:
public class VolatileCachedFactorizer extends GenericServlet implements Servlet {
private volatile OneValueCache cache = new OneValueCache(null, null);
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = cache.getFactors(i);
if (factors == null) {
factors = factor(i); //----------> thread A
cache = new OneValueCache(i, factors); //---------> thread B
}
encodeIntoResponse(resp, factors);
}
}
public class OneValueCache {
private final BigInteger lastNum;
private final BigInteger[] lastFactors;
public OneValueCache(BigInteger i, BigInteger[] lastFactors){
this.lastNum = i;
this.lastFactors …Run Code Online (Sandbox Code Playgroud) Google API python客户端对Django具有特殊的支持,在Google(https://developers.google.com/api-client-library/python/guide/django)提供的示例中,oauth2client.django_orm.Storage类为用于存储和检索凭据对象。
然而,我发现很多的例子(https://github.com/jgmize/django-google-drive/tree/master/gdrive/gdoauth2,https://github.com/praekelt/django-google-credentials/tree/ master / google_credentials等)将凭据放入用户个人资料的CredentialsField字段中,并将其保存到数据库中。
两种方法的优点/缺点是什么?有偏好吗?
谢谢
django google-api google-api-client oauth2client google-oauth