1

I managed to build a small REST API using eclipse. The following code works:

@Path("Info")
public class Rest {

@POST
@Path("/stats/{j}") 
@Produces("application/json")
public Response Status(@PathParam("j") String j) throws JSONException{
  JSONObject jsonObject = new JSONObject();
   String status = j;
   .
   .
 return Response.status(200).entity(result).build();
}
}

Could you advise me on how make this a multithreaded? I have an idea of what is multithreaded but I need some input on how to go about creating this code as multithreaded. was thinking of creating another class that implements Runnable:

class Demo implements Runnable {
  .
  .

}

Then, in my function Status(@PathParam("j") String j), I create an object of class Demo, For example:

public Response Status(@PathParam("j") String j) throws JSONException{
    Demo newThread = new Demo();
    JSONObject jsonObject = new JSONObject();
   String status = j;
   .
   .
 return Response.status(200).entity(result).build();
}
}

Thank you in advance!

1 Answer 1

12

It already is multithreaded.

When deploying the application into an application server such as Jetty or Tomcat the thread pool of the application determines how many threads will be used to serve web request. Every time a user makes a new web request against your controller method, one of the available threads from the application server threadpool will be used.

Sign up to request clarification or add additional context in comments.

11 Comments

just curios... for the default scope(Singleton?) of Rest class, one instance is shared between multiple threads or one thread uses one instance of Rest class(from the example)?
Hi Johan, thanks for the information. However, what I need is everytime I make a POST request it creates an object ad this object must not be destroyed until such time I have make a DELETE or CANCEL that request. So, there could be a lot request I made request1, request2, request3 etc...and I want these object/thread to keep running until i say CANCEL request3, or cancel request3 from my client. Thanks!
@user1143343, the class is shared.
@JoieTamayo then you need to stow that object away somewhere persitent between requests. Try storing it in the session or implement some service which does that for you.
Hi @Johan, thanks for your reply. I'm not that expert in java, could you please elaborate on "storing it in a session" ? Thanks in advance!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.