Need Deadlock in web application

Go To StackoverFlow.com

0

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);
            }

        }
    }

}

}

2012-04-04 02:05
by Tahani
I assume you exercised the app in such a way that you made sure that it would have to service two or more requests simultaneously? (in other words, you hit the same url from two separate browsers at the exact same time? deadlocks are a pain to repro - Kirk Woll 2012-04-04 02:09
I did what you said but nothing happene - Tahani 2012-04-04 03:16


0

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!

2012-04-05 00:47
by sharakan
can you please , edit the code above to show me where to add the sleep function , thank - Tahani 2012-04-08 07:58
Added Thread.sleep(60s) in appropriate places to cause a deadlock. If you can't get two web clients to hit within a minute, change the 60 * 1000 as necessary - sharakan 2012-04-08 12:58