REST API + Java多线程 [英] REST API + Java Multithreaded

查看:172
本文介绍了REST API + Java多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 {
  .
  .

}

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

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

提前谢谢!

推荐答案

它已经是多线程的。

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

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.

这篇关于REST API + Java多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆