Skip to content

Instantly share code, notes, and snippets.

@yuki-teraoka
Last active January 30, 2016 05:56
Show Gist options
  • Save yuki-teraoka/f7fc81803cf6bd1317b8 to your computer and use it in GitHub Desktop.
Save yuki-teraoka/f7fc81803cf6bd1317b8 to your computer and use it in GitHub Desktop.
package com.techscore.sandbox;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlets {
@WebServlet(name = "Index", urlPatterns = { "/" })
public static class Index extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("<html><body><h1>Sandbox!</h1>" + "<a href=\"/NetworkIO\">NetworkIO</a><br>"
+ "<a href=\"/DeadLock\">DeadLock</a><br>" + "<a href=\"/Regex\">Regex</a><br>");
}
}
@WebServlet(name = "Sleep", urlPatterns = { "/Sleep" })
public static class Sleep extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@WebServlet(name = "NetworkIO", urlPatterns = { "/NetworkIO" })
public static class NetworkIO extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
URL url = new URL("http://localhost:8080/Sleep");
InputStream is = url.openConnection().getInputStream();
}
}
@WebServlet(name = "DeadLock", urlPatterns = { "/DeadLock" })
public static class DeadLock extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object obj1 = new Object();
Object obj2 = new Object();
new Thread(() -> {
synchronized (obj1) {
sleep(500);
synchronized (obj2) {
}
}
}).start();
new Thread(() -> {
synchronized (obj2) {
sleep(500);
synchronized (obj1) {
}
}
}).start();
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@WebServlet(name = "Regex", urlPatterns = { "/Regex" })
public static class Regex extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
for (int i = 0; i < 50000000; i++) {
"hogehoge".replaceAll("hoge", "fuga");
}
resp.getWriter().println("Done!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment