Abbreviate qhelp example

This commit is contained in:
Chris Smowton
2021-11-24 12:12:22 +00:00
committed by GitHub
parent e56737e007
commit 136ecaf49a

View File

@@ -1,40 +1,11 @@
public class ThreadResourceAbuse extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get thread pause time from request parameter
String delayTimeStr = request.getParameter("DelayTime");
try {
int delayTime = Integer.valueOf(delayTimeStr);
new SyncAction(delayTime).start();
} catch (NumberFormatException e) {
}
}
class SleepTest {
public void test(int userSuppliedWaitTime) throws Exception {
// BAD: no boundary check on wait time
Thread.sleep(waitTime);
class SyncAction extends Thread {
int waitTime;
public SyncAction(int waitTime) {
this.waitTime = waitTime;
}
@Override
public void run() {
try {
{
// BAD: no boundary check on wait time
Thread.sleep(waitTime);
}
{
// GOOD: enforce an upper limit on wait time
if (waitTime > 0 && waitTime < 5000) {
Thread.sleep(waitTime);
}
}
//Do other updates
} catch (InterruptedException e) {
}
// GOOD: enforce an upper limit on wait time
if (waitTime > 0 && waitTime < 5000) {
Thread.sleep(waitTime);
}
}
}