title: What is the purpose of asynchronous JAX-RS
body: <p>I'm reading "RESTful Java with JAX-RS 2.0" book. I'm completely confused with asynchronous JAX-RS, so I ask all questions in one. The book writes asynchronous server like this:</p>
  
  <pre><code>@Path("/customers")
  public class CustomerResource {
  
      @GET
      @Path("{id}")
      @Produces(MediaType.APPLICATION_XML)
      public void getCustomer(@Suspended final AsyncResponse asyncResponse,
                              @Context final Request request,
                              @PathParam(value = "id") final int id) {
  
          new Thread() {
              @Override
              public void run() {
                  asyncResponse.resume(Response.ok(new Customer(id)).build());
              }
          }.start();
      }
  }
  </code></pre>
  
  <p>Netbeans creates asynchronous server like this:</p>
  
  <pre><code>@Path("/customers")
  public class CustomerResource {
      private final ExecutorService executorService = java.util.concurrent.Executors.newCachedThreadPool();
  
      @GET
      @Path("{id}")
      @Produces(MediaType.APPLICATION_XML)
      public void getCustomer(@Suspended final AsyncResponse asyncResponse, 
                              @Context final Request request,
                              @PathParam(value = "id") final int id) {
  
          executorService.submit(new Runnable() {
              @Override
              public void run() {
                  doGetCustomer(id);
                  asyncResponse.resume(javax.ws.rs.core.Response.ok().build());
              }
          });
      }
  
      private void doGetCustomer(@PathParam(value = "id") final int id) {
      }
  }
  </code></pre>
  
  <p>Those that do not create background threads use some locking methods to store response objects for further processing. This example is for sending stock quotes to clients:</p>
  
  <pre><code>@Path("qoute/RHT")
  public class RHTQuoteResource {
  
      protected List&lt;AsyncResponse&gt; responses;
  
      @GET
      @Produces("text/plain")
      public void getQuote(@Suspended AsyncResponse response) {
          synchronized (responses) {
              responses.add(response);
          }
      }
  }
  </code></pre>
  
  <p><code>responses</code> object will be shared with some background jobs and it will send quote to all clients when it is ready.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>In example 1 and 2 web server thread(the one that handle request) dies
  and we create another background thread. The whole idea behind
  asynchronous server is to reduce idle threads. These examples are
  not reducing idle threads. One threads dies and another one born.</li>
  <li>I thought creating unmanaged threads inside container is a bad idea.
  We should only use managed threads using concurrency utilities in
  Java EE 7.</li>
  <li>Again one of ideas behind async servers is to scale. Example 3 does not scale, does it?</li>
  </ol>