码迷,mamicode.com
首页 > 其他好文 > 详细

jcp.ch01.basic.CachedFactorizer

时间:2014-08-27 12:30:37      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   div   log   sp   new   

 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 }

 

jcp.ch01.basic.CachedFactorizer

标签:style   blog   color   java   io   div   log   sp   new   

原文地址:http://www.cnblogs.com/dadad/p/singleLock_two_state_var_vs_stack_var.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!