java并发编程问题
@ThreadSafepublicclassCachedFactorizerimplementsServlet{@GuardedBy("this")privateBigI...
@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();
}
}
if(factors == null) {
factors = factor(i);
synchronized(this) {
lastNumber = i;
lastFactors = factors.clone();
}
}
encodeIntoResponse(resp, factors);
}
}
请帮我解释一下这个程序,顺便加一些注释,谢谢! 展开
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();
}
}
if(factors == null) {
factors = factor(i);
synchronized(this) {
lastNumber = i;
lastFactors = factors.clone();
}
}
encodeIntoResponse(resp, factors);
}
}
请帮我解释一下这个程序,顺便加一些注释,谢谢! 展开
1个回答
展开全部
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CachedFactorizer implements Servlet { //java 网络访问中的缓存编程
// @GuardedBy is an annotation introduced by Brian Goetz in his excellent
// book on concurrency : Java Concurrency In Practice. The idea is that
// you indicate the lock that you need to hold before you access a particular
// member variable.
@GuardedBy("this") private BigInteger lastNumber;
//means you should synchronize on "this" before accessing lastNumber.
@GuardedBy("this") private BigInteger[] lastFactors;
//means you should synchronize on "this" before accessing lastFactors.
@GuardedBy("this") private long hits;
//means you should synchronize on "this" before accessing hits.
@GuardedBy("this") private long cacheHits;
//means you should synchronize on "this" before accessing 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;
//访问前面提到的那些敏感数据之前,先call synchronized(this) 同步
synchronized(this) {
++hits; //hit 自动加1
if(i.equals(lastNumber)) { //如果i和lastNumber是相等的话
++cacheHits; //cacheHits 自动加1
factors = lastFactors.clone(); //factors更新数据用lastFactors
}
}
if(factors == null) { //如果factors是空的话。
factors = factor(i); //更新factors的数据
synchronized(this) { //访问前面提到的那些敏感数据之前,先call synchronized(this) 同步
lastNumber = i; //更新lastNumber
lastFactors = factors.clone(); //lastFactors 用factors来更新
}
}
encodeIntoResponse(resp, factors); //取得resp然后返回对应数据
}
}
import javax.servlet.*;
import javax.servlet.http.*;
public class CachedFactorizer implements Servlet { //java 网络访问中的缓存编程
// @GuardedBy is an annotation introduced by Brian Goetz in his excellent
// book on concurrency : Java Concurrency In Practice. The idea is that
// you indicate the lock that you need to hold before you access a particular
// member variable.
@GuardedBy("this") private BigInteger lastNumber;
//means you should synchronize on "this" before accessing lastNumber.
@GuardedBy("this") private BigInteger[] lastFactors;
//means you should synchronize on "this" before accessing lastFactors.
@GuardedBy("this") private long hits;
//means you should synchronize on "this" before accessing hits.
@GuardedBy("this") private long cacheHits;
//means you should synchronize on "this" before accessing 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;
//访问前面提到的那些敏感数据之前,先call synchronized(this) 同步
synchronized(this) {
++hits; //hit 自动加1
if(i.equals(lastNumber)) { //如果i和lastNumber是相等的话
++cacheHits; //cacheHits 自动加1
factors = lastFactors.clone(); //factors更新数据用lastFactors
}
}
if(factors == null) { //如果factors是空的话。
factors = factor(i); //更新factors的数据
synchronized(this) { //访问前面提到的那些敏感数据之前,先call synchronized(this) 同步
lastNumber = i; //更新lastNumber
lastFactors = factors.clone(); //lastFactors 用factors来更新
}
}
encodeIntoResponse(resp, factors); //取得resp然后返回对应数据
}
}
追问
可以画一个简单的简单的流程图吗?
追答
画图太麻烦了,其实这段代码主要是里面有两个代码块:
其中一个同步代码块负责保护判断是否只需返回缓存结果的“先检查后执行”操作序列,
另一个同步代码块则负责确保对缓存的数值和引述分解结果进行同步操作。
此外,我们还重新引入了"命中计数器“,添加了一个”缓存命中“计数器,并且在第一个同步代码块中更新这两个变量。由于这两个计数器也是共享可变状态的一部分,因此必须在所有访问它们的位置上都是用同步。位于同步代码块之外代码将以独占方式来访问局部(位于栈中)变量,这些变量不会在多个线程间共享,因此不需要同步。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询