for some reason I want to check how the deadlock occurred in web application , so that's why I used the code below , but when I deploy the web application and test it , I did not get in the deadlock situation !! any help
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
public class DeadLockServlet extends HttpServlet
{
public static ArrayList student = new ArrayList();
public static ArrayList employee = new ArrayList();
PrintWriter out;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String lsAction = request.getParameter("action");
String lsValue = request.getParameter("data");
out = response.getWriter();
String msg = "";
if (lsAction != null)
{
if (lsAction.equals("addStudent"))
{
addStudent(lsValue);
msg = "Student added: "+lsValue;
}
else if (lsAction.equals("addEmployee"))
{
addEmployee(lsValue);
msg = "Employee added: "+lsValue;
}
}
else
{
msg = "Invalid Request";
}
request.setAttribute("msg", msg);
request.setAttribute("student", student);
request.setAttribute("employee", employee);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
public void addStudent(String lsValue)
{
synchronized (employee)
{
synchronized (student)
{
if (lsValue != null && !lsValue.equals(""))
{
student.add(lsValue);
}
}
}
}
public void addEmployee(String lsValue)
{
synchronized (student)
{
synchronized (employee)
{
if (lsValue != null && !lsValue.equals(""))
{
employee.add(lsValue);
}
}
}
}
}
Although your lock ordering will cause a deadlock, it requires exact timing. I don't think it's possible for you to do that without running that web server in the debugger and stepping through the code.
If you don't want to or can't do that, add a sufficiently long sleep between the two synchronized blocks in both addStudent and addEmployee. Say, 1 minute each. Then hit the servlet from two clients at around the same time, one doing addStudent, the other doing addEmployee.
The 'student' request handling thread will get the 'employee' lock, then sit and wait a bit. The 'employee' request handling thread will get the 'student' lock, then sit and wait. Then one of them will wake up, and try to grab the other lock, and the other thread will do the reverse, and presto: deadlock.
The add*** methods should be changed to something like this:
public void addStudent(String lsValue)
{
synchronized (employee)
{
Thread.sleep(60 * 1000);
synchronized (student)
{
if (lsValue != null && !lsValue.equals(""))
{
student.add(lsValue);
}
}
}
}
public void addEmployee(String lsValue)
{
synchronized (student)
{
Thread.sleep(60 * 1000);
synchronized (employee)
{
if (lsValue != null && !lsValue.equals(""))
{
employee.add(lsValue);
}
}
}
}
Obviously deadlocks are to be avoided, but hey, that's a whole nother question!