jcp.ch01.basic.CachedFactorizer

 1 package book.jcp.ch01.basic;
 2 
 3 import java.math.BigInteger;
 4 
 5 public class CachedFactorizer {
 6 
 7     private BigInteger lastNumberBigInteger;
 8     private BigInteger[] lastFactorsBigIntegers;
 9     private long hits;
10     private long cacheHits;
11 
12     public synchronized long getHits() {
13         return hits;
14     }
15 
16     public synchronized double getHitRatio() {
17         return (double) cacheHits / (double) hits;
18     }
19 
20     public void service(int req, int resp) {
21         BigInteger i = extractFromRequest(req);
22         BigInteger[] newFactors = null;
23         synchronized (this) {
24             ++hits;
25             if (i.equals(lastNumberBigInteger)) {
26                 ++cacheHits;
27                 newFactors = lastFactorsBigIntegers.clone();
28             }
29         }
30         if (newFactors == null) {
31             newFactors = factor(i);
32             synchronized (this) {
33                 lastNumberBigInteger = i;
34                 lastFactorsBigIntegers = newFactors.clone();
35             }
36         }
37         encodeIntoResponse(resp, newFactors);
38     }
39 
40     private BigInteger[] factor(BigInteger i) {
41         // TODO Auto-generated method stub
42         return null;
43     }
44 
45     private void encodeIntoResponse(int resp, BigInteger[] factors) {
46         // TODO Auto-generated method stub
47 
48     }
49 
50     private BigInteger extractFromRequest(int req) {
51         // TODO Auto-generated method stub
52         return null;
53     }
54 }

 

转载于:https://www.cnblogs.com/dadad/p/singleLock_two_state_var_vs_stack_var.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值