- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请查看以下类(class),建议使用哪一个类(class)以及它们有何不同..?
// Approach 1
public class Test extends Controller {
private final DatabaseExecutionContext executionContext;
@Inject
public Test(DatabaseExecutionContext executionContext) {
this.executionContext = executionContext;
}
public CompletableFuture<Result> get() throws ExecutionException, InterruptedException {
return io().thenApply(Results::ok);
}
public CompletableFuture<String> io() throws ExecutionException, InterruptedException {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "io";
}, executionContext);
}
}
//Approach 2
public class Test extends Controller {
private final DatabaseExecutionContext executionContext;
@Inject
public Test(DatabaseExecutionContext executionContext) {
this.executionContext = executionContext;
}
public Result get() throws ExecutionException, InterruptedException {
return Results.ok(io());
}
public String io() throws ExecutionException, InterruptedException {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "io";
}, executionContext).join();
}
}
我想了解如何正确处理阻塞 IO 调用。有人能解释一下 Play 框架如何使用 Completable Future,在我看来它仍然是阻塞的并且没有多大意义,并且让我的代码处理本身很糟糕的 CompletableFuture。
最佳答案
Play 对我来说是新手,我想了解如何正确处理阻塞 IO 调用。
嗨!很好的问题。我一直在使用 Play!一段时间以来,我认为您已经无意中遇到了 Play! 中最复杂的问题之一!不得不提供。让我们使用官方游戏吧!文档回答你的问题:
The default configuration is tuned for asynchronous controllers. In other words, the application code should avoid blocking in controllers, i.e., having the controller code wait for an operation. Common examples of such blocking operations are JDBC calls, streaming API, HTTP requests and long computations. [1]
所以回答你的问题:很简单,根本不要使用阻塞调用。 :) 如果您仍想阻止调用,请继续阅读。
谁能解释一下 Play 框架如何使用 Completable Future,在我看来,它仍然是阻塞的,没有多大意义,让我的代码处理本身很糟糕的 CompletableFuture。
您提供的两个示例确实是阻塞的。它们之间有一个细微的差别。带有 CompletableFuture 的 get()
方法会立即退出,没有的 get()
方法不会退出。它将等待阻塞调用完成。第一个将发布 Play!资源来做其他事情,比如处理其他请求。
由于您正在进行阻塞调用,因此您可能需要设计一个关于如何处理它们的策略:
Play Framework is, from the bottom up, an asynchronous web framework. Streams are handled asynchronously using iteratees. Thread pools in Play are tuned to use fewer threads than in traditional web frameworks, since IO in play-core never blocks.
Because of this, if you plan to write blocking IO code, or code that could potentially do a lot of CPU intensive work, you need to know exactly which thread pool is bearing that workload, and you need to tune it accordingly. Doing blocking IO without taking this into account is likely to result in very poor performance from Play Framework, for example, you may see only a few requests per second being handled, while CPU usage sits at 5%. In comparison, benchmarks on typical development hardware (eg, a MacBook Pro) have shown Play to be able to handle workloads in the hundreds or even thousands of requests per second without a sweat when tuned correctly. [2]
这里的文档再次帮助您设计此策略:Best practices for ThreadPools
如果您的应用程序是超高同步的,您可以忘记 CompletableFuture 方法并使用此处描述的方法:https://www.playframework.com/documentation/2.8.x/ThreadPools#Highly-synchronous
关于java - 处理 Play Framework 中的阻塞 IO 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501520/
对于一个简单的聊天程序,我使用了一个通过 boost::python 包装的 c 库。 使用 PyQT 编写了一个简单的 GUI。接收消息是通过阻塞调用完成的lib说。对于独立刷新的 GUI,通信部分
当我创建以下内容时,我试图创建一个可以被异常终止的线程类(因为我试图让线程等待一个事件): import sys class testThread(threading.Thread): def
我正在用 Haskell 编写服务器,我想在客户端断开连接后显式关闭它们。当我调用 hClose ,线程将阻塞,直到客户端关闭其一侧的句柄。有没有办法让它在不阻塞的情况下关闭? 提前致谢! 最佳答案
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: garbage collection Operation 我有几个相关问题。 1.JAVA垃圾收集器运行时,是否占用
我有一个 Angular 函数,它在初始 URL 中查找“列表”参数,如果找到,就会出去获取信息。否则我想获得地理位置。如果存在 URL 参数,我不想获取地理位置。我使用的术语是否正确? constr
我读了很多关于锁定数据库、表和行的文章,但我想要较低的锁定,比如只锁定“操作”,我不知道如何调用它,假设我在 php 中有函数: function update_table() { //que
在我的多线程 mfc 应用程序中,m_view->SetScrollPos 处于阻塞状态并且所有应用程序都被卡住。 View 是在另一个线程中创建的,这是这种行为的原因吗? //SetScrollPo
FreeSwitch 软件在几天内运行良好(~3 - 5 天),然后由于 FreeSwitch 被阻止,新的来电请求被接受!!正在进行的调用继续他们的 session ,他们的调用似乎没有受到影响,但
我有一组按钮,当鼠标悬停在这些按钮上时,它们会改变颜色。这些的 CSS 以这种方式运行: #navsite ul li button { height: 60px; width: 60
由于某些原因,当我调用 WSARecvFrom 时,该函数在接收到某些内容之前不会返回。 _socket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, N
我了解一些关于 Oracle 阻塞的知识——更新如何阻塞其他更新直到事务完成,写入者如何不阻塞读取者等。 我理解悲观和乐观锁定的概念,以及有关丢失更新等典型银行教科书示例。 我也理解 JDBC 事务隔
在两个代码点之间,我是否可以判断进程是否已被内核抢占,或者更确切地说,当时是否有任何其他代码在同一处理器上运行? //Point A some_type capture = some_capture(
这是我在 Oracle 的面试问题。 有一个堆栈,即使堆栈已满,push 操作也应该等到它完成,即使堆栈为空,pop 操作也应该等到它完成。 我们怎样才能做到这一点? 我的回答 让一个线程做push
我想知道是否有人可以告诉我如何有效地使用循环平铺/循环阻塞进行大型密集矩阵乘法。我正在用 1000x1000 矩阵做C = AB。我按照 Wikipedia 上的循环平铺示例进行操作,但使用平铺得到的
我正在阅读有关绿色线程的内容,并且能够理解这些线程是由 VM 或在运行时创建的,而不是由操作系统创建的,但我无法理解以下语句 When a green thread executes a blocki
我正在创建的 JavaScript API 具有以下结构: var engine = new Engine({ engineName: "TestEngine", engineHost
ChildWindow 是一个模态窗口,但它不会阻塞。有没有办法让它阻塞?我基本上想要一个 ShowDialog() 方法,该方法将调用 ChildWindow.Show() 但在用户关闭 Child
我需要一些关于如何调试 10.6 版本下的 Cocoa 并发问题的指导。我正在将“for”循环转换为使用 NSOperations,但大多数时候,代码只是在循环的某个时刻卡住。我可以在控制台中看到 N
我正在使用 ReportViewer 控件和自定义打印作业工作流程,这给我带来了一些问题。我的代码看起来有点像这样: ids.ForEach(delegate(Guid? guid)
我有以下成功复制文件的代码。但是,它有两个问题: progressBar.setValue() 之后的 System.out.println() 不会打印 0 到 100 之间的间隔(仅打印“0”直到
我是一名优秀的程序员,十分优秀!