/** * * @author weip 书中讲到计数器count是线程安全(毋庸置疑,AtomicLong是命令级的线程安全), * 而计数器的状态就是servlet的状态,这是为何? * */ @ThreadSafe public class CountingFactorizer extends GenericServlet implements Servlet { private final AtomicLong count = new AtomicLong(0); public long getCount() { return count.get(); } public void service(ServletRequest req, ServletResponse resp) { BigInteger i = extractFromRequest(req); BigInteger[] factors = factor(i); count.incrementAndGet(); encodeIntoResponse(resp, factors); } void encodeIntoResponse(ServletResponse res, BigInteger[] factors) { } BigInteger extractFromRequest(ServletRequest req) { return null; } BigInteger[] factor(BigInteger i) { return null; } }
时间: 2024-11-04 17:48:04