Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:35:22+00:00 2026-06-01T08:35:22+00:00

for some reason I want to check how the deadlock occurred in web application

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

        }
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:35:24+00:00Added an answer on June 1, 2026 at 8:35 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check if the day is Sunday, but for some reason I
My server for some reason displays .AIR file inside the browser, but I want
I'm now developing an app on iOS. With some reason, I want to use
For some reason that I don't understand, when I create my SurfaceView from an
For some reason my image button keeps overlapping. When i used the Scroll view
I want to run some code before an object is removed from a has_many
I want to access a custom attribute that I added to some elements in
My application uses a database and at the beginning I want to check if
This should be a simple matter, but for some reason I can't grasp it.
I might be doing something completely wrong here but for some reason the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.