title: Process multiple request at same time
body: <p>I have a service class</p>
  
  <pre><code>    @Path("/")
       public class ABC {
        @Path(/process/{param})
        public String processRequest(@PathParam("param") String param){
          Thread t = new Thread(()-&gt;{
           // Do some processing with the param parmeter
            System.out.println("Processing started for -&gt; "+ param);
            //Do many more things
            //DB transactions,etc.
             });
            t.start();
            return "Your request will be processed";
          }
  }
  </code></pre>
  
  <p>I accept some parameter and start processing it in a new thread and at the same time so that it should complete the processing within 30 secs, I break my connection with the client by acknowledging him that his request will be processed.</p>
  
  <p>It works fine and till now without any issues, Currently, it can process more than 5k requests. The problem starts when there is a lot of requests come at the same time maybe more than 50k so my application creates a new thread for every new request which causes the application to allocate a lot of memory and also sometimes makes JVM memory to exhaust.</p>
  
  <p>Is there any another way by which I can immediately start the processing without bothering for the number of requests and process all the requests within 30 secs and also limit the no of threads active working threads.</p>
  
  <p>One way I found was the Producer-Consumer implementation in which I can accept all the requests and put simultaneously into the producers and my consumers pick up the request and start processing it, For this implementation i need to specify the maximum no of request which can be accepted by producer(Ex : 100 000) and no of consumers which can process the request(Ex : 1000) so that only 1000 threads are active and process one after another but issue with this approach is that if any of the consumer (working) thread if locks due to some reason and if not released then there are only the remaining unlocked threads left to process the request and the incoming request is continuously increasing in the producers. Only increasing the no of consumers creates more working thread but at the same time there can be a lot of locked threads processing the task.</p>
  
  <p>Please let me know any another approach by which I can do it.</p>
  
  <p><b>Note : All the request should be processed within 30 secs and if unable to do then it fails the success criteria.</b> </p>