我有很多类似这样的电话。问题是下一个调用完全取决于前一个调用。如果没有任何对话,从他们那里获取消息是没有意义的,所以我只想打破这个链条。我读了一些霍尔格答案的主题,但我觉得我仍然没有完全理解这一点。有人可以给我一些基于这段代码的例子吗?
public CompletableFuture<Set<Conversation>> fetchConversations(List<Information> data, String sessionId)
{
return myservice
.get(prepareRequest(data, sessionId))
.thenApply(HtmlResponse::getDocument)
.thenApply(this::extractConversationsFromDocument);
}
public CompletableFuture<Elements> fetchMessagesFromConversation(String Url, String sessionId)
{
return mySerice
.get(prepareRequest(url, sessionId))
.thenApply(HtmlResponse::getDocument)
.thenApply(this::extractMessageFromConversation);
}
Run Code Online (Sandbox Code Playgroud) 我想知道 Reactor 类型(Mono 和 Flux)或什至 Rxjava 类型是否是 Java 的 Completable Future 的包装器,或者它们是否使用自己的实现。
任何有关此事的帮助将不胜感激
java asynchronous reactive-programming project-reactor completable-future
我有一个方法,它首先执行一系列操作,然后启动异步任务。我想测试这个方法,但我不明白如何验证异步操作是否已完成。
\n\n使用 Mo\xd1\x81kito,我想验证foo方法是否执行了两次,一次是在异步任务开始之前,一次是在异步任务内部。问题是,在 Mockito 检查时,异步任务可能尚未调用异步操作内的方法。因此,有时进行测试,有时不进行测试。
\n\n这是我的方法的示例:
\n\nvoid testingMethod() {\n // some operations\n someObject.foo();\n CompletableFuture.runAsync(() -> {\n // some other operations\n someObject.foo();\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的测试示例,其中 someObject 被嘲笑:
\n\n@Test\npublic void testingMethodTest() {\n testObject.testingMethod();\n\n Mockito.verify(someObject, Mockito.times(2)).foo();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n有没有办法在验证方法之前等待异步操作完成。或者这是一种不好的测试方法,在这种情况下您有什么建议?
\n想知道 completablefuture 是否可以在创建它的线程中运行。您可能会问为什么我需要这样做,因为 completablefuture 是用于异步编程的。原因是我有一些异步任务和一些我想在生成线程中运行的任务,以便我可以使用 allOf 等并保持代码的一致性
我正在开发一个包含很多CompletableFuture.completedFuture ... thenAccept代码的项目,例如
public CompletableFuture<Boolean> callee() {
boolean result = ... // Do something and get result - Step A
return CompletableFuture.completedFuture(Boolean.valueOf(result));
}
public void caller() {
callee().thenAccept(result -> {
// Detect if call success or failure - Step B
new Throwable().printStackTrace(); // the debug code: stacktrace shows it is called from caller
});
}
Run Code Online (Sandbox Code Playgroud)
我得出的结论是,步骤 A 和步骤 B 在一个线程中顺序调用。
那么我可以这样简化吗?
public boolean callee() {
boolean result = ... // Do something and get result
return result;
} …Run Code Online (Sandbox Code Playgroud) 我有一个 Rest api,它在其中调用异步调用,如下所示
CompletableFuture.runAsync(() -> {
// method call or code to be async.
try {
logger.info("======Before Async method call======with studySchemaEventId: "+enrollmentStudySchemaEventId);
this.performSimulation(studyId, enrollmentStudySchemaEventId, cloneOfFile, simulationRunInfo, totalAccrual);
logger.info("======After Async method call======with studySchemaEventId: "+enrollmentStudySchemaEventId);
} catch (SimulationException e) {
logger.error("Error running Async call for performSimulation()", e);
}
});
Run Code Online (Sandbox Code Playgroud)
当我调用 Rest api 时,它正确执行了异步调用。但我有一个情况,我调用 Rest Api 4 次,它执行了 3 次异步调用,而对于第四次 Api 调用,我没有看到异步方法被调用。
runAsync() 调用有任何限制吗?或者为什么在 3 次调用后不调用 Async 方法?
这是 Rest API 调用:
@POST
@Path("/trigger")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Trigger Simulation", tags = "Study Event …Run Code Online (Sandbox Code Playgroud) java multithreading asynchronous threadpool completable-future
我有一个调用两个单独线程的线程。CompletableFuture它向这两个子线程传递相同的内容。如果.get()同时在这两个线程中调用,我会遇到任何类型的并发问题吗?
CompletableFuture?.get()?作为一个具体的例子,在下面的代码中,假设完成cfInput.get()后返回的对象没有发生任何变化,两个线程是否有可能打印不同的值?cfInput
public void mainClass(CompletableFuture<ObjA> cfInput){\n class1.doAsync1(cfInput);\n class2.doAsync2(cfInput);\n}\n\n@Async\npublic void doAsync1(CompletableFuture<ObjA> cfInput){\n //logic\n System.out.println(cfInput.get().getObjB().getBlah());\n //logic\n}\n\n@Async\npublic void doAsync2(CompletableFuture<ObjA> cfInput){\n //logic\n System.out.println(cfInput.get().getObjB().getBlah());\n //logic\n}\n\npublic class ObjA{\n private ObjB objB;\n public ObjB getObjB();\n public void setObjB();\n}\npublic class ObjB{\n private String blah;\n public String getBlah();\n public void setBlah();\n}\nRun Code Online (Sandbox Code Playgroud)\n getPrice()由于以下错误,我在使用方法返回汽车价格值时遇到问题:
no instance(s) of type variable(s) U exist so that CompletableFuture<U> conforms to Double inference variable U has incompatible bounds: equality constraints: Double lower bounds: CompletableFuture<U81345>
Run Code Online (Sandbox Code Playgroud)
我想要getPrice返回CompletableFuture<Double>,但它返回了CompletableFuture<CompletableFuture<Double>>,因为我正在从嵌套的 future 返回一个值。我可以调用.join()嵌套的 future,但我不想阻塞线程。这是我的代码:
no instance(s) of type variable(s) U exist so that CompletableFuture<U> conforms to Double inference variable U has incompatible bounds: equality constraints: Double lower bounds: CompletableFuture<U81345>
Run Code Online (Sandbox Code Playgroud) 我在使用CompletableFuture将代码转换为非阻塞代码时遇到了问题.为了最小化问题的范围,我创建了一个示例代码,当我使用CompletableFuture时,该代码的行为有所不同.问题是CompletableFuture从Runnable-delegation吞下异常.
我在Runnable和ExecutorService之上使用委托来提供我原始应用程序中所需的一些包装代码.
示例代码:
MyRunnable:我的示例runnable,它总是抛出异常.
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("This is My Thread throwing exception : " + Thread.currentThread().getName());
throw new RuntimeException("Runtime exception from MyThread");
}
}
Run Code Online (Sandbox Code Playgroud)DelegatingRunnable - 这是委托runnable,它委托并包装传递给它的Runnable的逻辑,以及用于异常处理的占位符.
public class DelegatingRunnable implements Runnable {
private Runnable delegate;
public DelegatingRunnable(Runnable delegate) {
this.delegate = delegate;
}
@Override
public void run() {
System.out.println("Delegating Thread start : " + Thread.currentThread().getName());
try {
// Some code before thread execution
delegate.run();
// Some …Run Code Online (Sandbox Code Playgroud)我想创建一个具有在Kotlin中特定执行程序上运行的返回值的CompletableFuture。
以下代码可以正常工作。
return CompletableFuture.supplyAsync {
val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
commandHandler.handle(command)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过执行程序时,它将无法编译。
return CompletableFuture.supplyAsync({
val commandHandler = registry.get<TCommand, TResponse>(command::class.java)
commandHandler.handle(command)
}, exec)
Run Code Online (Sandbox Code Playgroud)
我试图变得更聪明,并编写了Java版本,并把Intellij隐瞒了给Kotlin,但是那个也有同样的错误。我在这里做错了什么?
编辑:
我可以通过执行以下操作使其工作,但这似乎不必要。有人可以解释为什么这样做有效,而其他方法却无效。还有其他编写此代码的方法吗?
return CompletableFuture.supplyAsync(Supplier {
commandHandler.handle(command)
}, exec)
Run Code Online (Sandbox Code Playgroud) java ×9
asynchronous ×3
java-8 ×2
future ×1
junit5 ×1
kotlin ×1
mockito ×1
spring-async ×1
threadpool ×1