Java Web服务器-用于逐步IO操作的并行工作者设计与装配线设计
我正在编写一个java服务器接口,它包含了http请求的几个步骤,每个步骤需要大约10毫秒才能完成,最终结果不会返回给客户端。方法如下:Java Web服务器-用于逐步IO操作的并行工作者设计与装配线设计,java,multithreading,concurrency,Java,Multithreading,Concurrency,我正在编写一个java服务器接口,它包含了http请求的几个步骤,每个步骤需要大约10毫秒才能完成,最终结果不会返回给客户端。方法如下: public void printUserBookList(int userId){ var user = userService.getUser(userId); // http req taking 10 ms to finish var bookShelf = bookShelfService.getShelf(user); // http re
public void printUserBookList(int userId){
var user = userService.getUser(userId); // http req taking 10 ms to finish
var bookShelf = bookShelfService.getShelf(user); // http req taking 10 ms to finish
var books = bookService.getBooks(bookShelf); // http req taking 10 ms to finish
printService.print(books); // http req taking 10 ms to finish
}
为了获得更好的吞吐量和性能,我应该使用哪种并发模型
threadpoolexecutor
演员风格设计:
public void printUserBookList(int userId){
userActor.getUserAndThenGoNextStepAsync(userId);
// inside getUserAndThenGoNextStepAsync, IO operations are executed by another threadPoolExecutor
// and the result will be passed to the successors for further async executions
}
对于普通的tomcat服务器,哪种设计可以保证更好的吞吐量和性能?全异步设计会更好地利用CPU使用率吗?增加额外的线程池(特别是为每个步骤单独设置一个线程池)不太可能为您提供任何性能优势。如果您想测试它,您应该尝试使用chained
CompatibleFutures
进行步骤。然后,您可以轻松地使用不同的线程池设置进行测试。虽然这种方法需要40毫秒,但我不会太担心效率。即使是一个线程也会给您30个操作/秒。不清楚同一请求的不同步骤是否可以并行执行。@AlexeiKaigorodov不,不同的步骤仍然应该一步一步执行,但它们都像单向消息,使用第一个变量。第二种变体只会增加更多的开销。