package pub.capart;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GrindServlet extends HttpServlet {
PRotected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Grinderv1 grinder = Grinderv1.getGrinder();
long t1 = System.currentTimeMillis();
grinder.grindCPU(13);
long t2 = System.currentTimeMillis();
PrintWriter pw = res.getWriter();
pw.print("<Html>/n< body> /n");
pw.print("Grind Time = "+(t2-t1));
pw.print("< body> /n< /html> /n");
}
}
package pub.capart;
/**
* This is a simple class designed to simulate an application consuming
* CPU, memory, and contending for a synchronization lock.
*/
public class Grinderv1 {
private static Grinderv1 singleton = new Grinderv1();
private static final String randstr =
"this is just a random string that I'm going to add up many many times";
public static Grinderv1 getGrinder() {
return singleton;
}
public synchronized void grindCPU(int level) {
StringBuffer sb = new StringBuffer();
String s = randstr;
for (int i=0;i<level;++i) {
sb.append(s);
s = getReverse(sb.toString());
}
}
public String getReverse(String s) {
StringBuffer sb = new StringBuffer(s);
sb = sb.reverse();
return sb.toString();
}
}
新闻热点
疑难解答