package cn.dayup.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.dayup.vo.ExtensiveObject;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.NotThreadSafe;
import net.jcip.annotations.ThreadSafe;
public class MyThreadSafe extends HttpServlet {
/**
* Constructor of the object.
*/
public MyThreadSafe() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 无状态
* @author Amy
*
*/
@ThreadSafe
public class StatelessFactorizer implements Servlet{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
encodeIntoResponse(resp,factors);
}
}
/**
* 线程不安全
* @author Amy
*
*/
@NotThreadSafe
public class UnsafeCountingFactorizer implements Servlet{
private long count = 0;
public long getCount(){return count;}
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
++count;
encodeIntoResponse(resp,factors);
}
}
/**
* 惰性初始化
* @author Amy
*
*/
@NotThreadSafe
public class LazyInitRace{
private ExtensiveObject instance = null;
public ExtensiveObject getInstance(){
if(instance == null)
instance = new ExtensiveObject();
return instance;
}
}
/**
* 原子化操作
* @author Amy
*
*/
@ThreadSafe
public class CountingFactorizer implements Servlet{
private final AtomicLong count = new AtomicLong(0);
public long getCount(){return count.get();}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
count.incrementAndGet();
encodeIntoResponse(resp, factors);
}
}
/**
* 没有正确原子化得Servlet试图缓存它的最新结果
* @author Amy
*
*/
public class UnsafeCachingFactorizer implements Servlet{
private final AtomicReference<BigInteger> lastNumber = new AtomicReference<BigInteger>();
private final AtomicReference<BigInteger[]> lastFactors = new AtomicReference<BigInteger[]>();
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
BigInteger i = extractFromRequest(req);
if(i.equals(lastNumber.get())){
encodeIntoResponse(resp, lastFactors.get());
}else{
BigInteger[] factors = factor(i);
lastNumber.set(i);
lastFactors.set(factors);
encodeIntoResponse(resp, factors);
}
}
private void encodeIntoResponse(ServletResponse resp,
BigInteger[] factors) {
// TODO Auto-generated method stub
}
private void encodeIntoResponse(ServletResponse resp,
BigInteger bigInteger) {
// TODO Auto-generated method stub
}
}
/**
* 利用synchronized来保证线程安全,但是性能不好
* @author Amy
*
*/
public class SynchronizedFactorizer implements Servlet{
/*
* @GuardedBy( lock )有以下几种使用形式:
* 1、@GuardedBy( "this" ) 受对象内部锁保护
* 2、@GuardedBy( "fieldName" ) 受 与fieldName引用相关联的锁 保护。
* 3、@GuardedBy( "ClassName.fieldName" ) 受 一个类的静态field的锁 保存。
* 4、@GuardedBy( "methodName()" ) 锁对象是 methodName() 方法的返值,受这个锁保护。
* 5、@GuardedBy( "ClassName.class" ) 受 ClassName类的直接锁对象保护。而不是这个类的某个实例的锁对象。
*/
@GuardedBy("this") private BigInteger lastNumber;
@GuardedBy("this") private BigInteger[] lastFactors;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public synchronized void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
BigInteger i = extractFromRequest(req);
if(i.equals(lastNumber))
encodeIntoResponse(resp, lastFactors);
else{
BigInteger[] factors = factor(i);
lastNumber = i;
lastFactors = factors;
encodeIntoResponse(resp, factors);
}
}
}
/**
* 如果内部锁不是可重入,代码将死锁
* @author Amy
*
*/
public class Widget{
public synchronized void doSomething(){
}
}
public class LoggingWidget extends Widget{
public synchronized void doSomething(){
System.out.println(toString()+":calling doSomething");
super.doSomething();
}
}
/**
* 缓存最新请求和结果
* @author Amy
*
*/
public class CacheFactorizer implements Servlet{
@GuardedBy("this") private BigInteger lastNumber;
@GuardedBy("this") private BigInteger[] lastFactors;
@GuardedBy("this") private long hits;
@GuardedBy("this") private long cacheHits;
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
}
public synchronized long getHits(){return hits;}
public synchronized double getCacheHitRatio(){
return (double) cacheHits /(double) hits;
}
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
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(res, factors);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
public void encodeIntoResponse(ServletResponse resp, BigInteger[] factors) {
// TODO Auto-generated method stub
}
public BigInteger[] factor(BigInteger i) {
// TODO Auto-generated method stub
return null;
}
public BigInteger extractFromRequest(ServletRequest req) {
// TODO Auto-generated method stub
return null;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
有关并发的代码(未整理完)
最新推荐文章于 2023-08-30 22:54:20 发布