首页 > 代码库 > jcp.ch01.basic.CachedFactorizer
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 stub42 return null;43 }44 45 private void encodeIntoResponse(int resp, BigInteger[] factors) {46 // TODO Auto-generated method stub47 48 }49 50 private BigInteger extractFromRequest(int req) {51 // TODO Auto-generated method stub52 return null;53 }54 }
jcp.ch01.basic.CachedFactorizer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。