Create deadlock situation in servlet

Create deadlock situation in servlet

A deadlock is a situation in computer programming where two or more threads are blocked forever, waiting for each other to release a resource. Deadlocks can occur in a servlet if synchronized blocks are improperly used. This typically happens when multiple threads (requests) are waiting for resources held by each other, leading to a standstill.

In the context of servlets, a deadlock can occur if:

  1. Two or more servlets attempt to obtain locks on the same set of resources but in different orders.
  2. Synchronized blocks are used incorrectly in a way that one thread is waiting for another thread to release a lock it is holding.

Java Example

Below is a servlet example that demonstrates a deadlock situation. Two servlets (ServletA and ServletB) each hold a lock on one resource and attempt to acquire a lock on the other resource, leading to a deadlock.

deadlock situation

ServletA
    java
    package com.example;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    @WebServlet("/servletA")
    public class ServletA extends HttpServlet {
        private final Object resource1 = new Object();
        private final Object resource2 = new Object();

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            synchronized (resource1) {
                try {
                    // Simulate some work with resource1
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Trying to get a lock on resource2
                synchronized (resource2) {
                    response.getWriter().println("ServletA acquired both locks");
                }
            }
        }
    }
    

ServletB :
    java
    package com.example;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    @WebServlet("/servletB")
    public class ServletB extends HttpServlet {
        private final Object resource1 = new Object();
        private final Object resource2 = new Object();

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            synchronized (resource2) {
                try {
                    // Simulate some work with resource2
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Trying to get a lock on resource1
                synchronized (resource1) {
                    response.getWriter().println("ServletB acquired both locks");
                }
            }
        }
    }

Detailed deadlock situation in servlet

1.  Resource Locking

  • ServletA first acquires a lock on resource1 and then attempts to acquire a lock on resource2.
  • ServletB first acquires a lock on resource2 and then attempts to acquire a lock on resource1.

2.  Deadlock Scenario

  1. If ServletA and ServletB are accessed simultaneously by different requests, ServletA will hold the lock on resource1 and wait for resource2, while ServletB holds the lock on resource2 and waits for resource1.
  2. This circular waiting creates a deadlock where neither servlet can proceed, causing both requests to hang indefinitely.

3.  Simulation

   The Thread.sleep(100); simulates a delay to increase the likelihood of a deadlock occurring by giving each thread time to reach the second synchronized block while holding the first lock.

Homepage

Readmore