相关疑难解决方法(0)

"实践中的Java并发" - 缓存的线程安全数字因子(清单2.8)

在下面的代码中(从Java Concurrency in Practice第2章,第2.5节,清单2.8中复制):

@ThreadSafe
public class CachedFactorizer implements Servlet {
    @GuardedBy("this") private BigInteger lastNumber;
    @GuardedBy("this") private BigInteger[] lastFactors;
    @GuardedBy("this") private long hits;
    @GuardedBy("this") private long cacheHits;

    public synchronized long getHits() { return hits; }

    public synchronized double getCacheHitRatio() {
        return (double) cacheHits / (double) hits;
    }

    public void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        BigInteger[] factors = null;
        synchronized (this) {
            ++hits;
            if (i.equals(lastNumber)) {
                ++cacheHits;
                factors = lastFactors.clone(); // questionable line here
            } …
Run Code Online (Sandbox Code Playgroud)

java clone local-variables thread-safety

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

标签 统计

clone ×1

java ×1

local-variables ×1

thread-safety ×1