REST API + Javamultithreading

我设法使用eclipse构建一个小型REST API。 以下代码有效:

@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(); } } 

你能告诉我如何使它成为multithreading吗? 我知道什么是multithreading但我需要一些关于如何创建multithreading代码的输入。 正在考虑创建另一个实现Runnable的类:

 class Demo implements Runnable { . . } 

然后,在我的函数Status(@PathParam(“j”)String j)中,我创建了一个Demo类的对象,例如:

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

先感谢您!

它已经是multithreading的。

将应用程序部署到应用程序服务器(如Jetty或Tomcat)时,应用程序的线程池将确定将使用多少线程来提供Web请求。 每次用户针对您的控制器方法发出新的Web请求时,都将使用应用程序服务器线程池中的一个可用线程。