小编yun*_*shi的帖子

volatile + immutable holder object =线程安全吗?

我从"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)

java volatile immutability thread-safety java-memory-model

7
推荐指数
1
解决办法
746
查看次数

如何在django中存储google oauth令牌:存储或数据库

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/gdoauth2https://github.com/praekelt/django-google-credentials/tree/ master / google_credentials等)将凭据放入用户个人资料的CredentialsField字段中,并将其保存到数据库中。

两种方法的优点/缺点是什么?有偏好吗?

谢谢

django google-api google-api-client oauth2client google-oauth

5
推荐指数
1
解决办法
1092
查看次数