与返回值的Java中的并发编程
我在Java中同时编程有问题。我正在研究我的学士学位论文,我必须制作几种方法,这些方法将使我归还一个字符串价值。在期货中,TriggerMessageFututre和GetMetervaluesfuture是一个过程运行,需要在1-5秒之间进行返回字符串值完成后返回。
现在的问题是Future.get()正在阻止我的主线程。我想在我的主线中调用触发器和getMetervalue methode,而不会阻止我的主线程,并在完成后将答案作为返回值。我无法找到解决问题的方法,因为它是没有返回值的解决方案,或者是阻止线程的解决方案。
private String TriggerMessage(String Messagetyp) throws InterruptedException, ExecutionException{
Future<String> future = new communicator().TriggerMessageFuture(queue,centralSystem,Messagetyp);
while(!future.isDone()) {
System.out.println("[TriggerMessage]: Calculating... ");
Thread.sleep(500);
}
String result = future.get(); //blocking
return result;
}
private String getMeterValue(String key) throws Exception{
Future<String> future = new communicator().getMeterValueFuture(queue,centralSystem,key);
while(!future.isDone()) {
System.out.println("[getMeterValue]: Calculating...");
Thread.sleep(500);
}
String result = future.get(); //blocking
return result;
}
I have a problem with concurrent programming in Java. I am working on my bachelor thesis and I have to make several methods which will return me a String value. In the Futures TriggerMessageFututre and getMeterValuesFuture is a process running which takes between 1-5 seconds and returns a String Value when it's finished.
The problem is now that future.get() is blocking my main thread. I want to call the TriggerMessage and the getMeterValue methode in my main without blocking my main thread and get their answer as a return value when they are finished. I wasn't able to find a way to solve my problem, because either it was a solution without return value or it was a solution which blocked the thread.
private String TriggerMessage(String Messagetyp) throws InterruptedException, ExecutionException{
Future<String> future = new communicator().TriggerMessageFuture(queue,centralSystem,Messagetyp);
while(!future.isDone()) {
System.out.println("[TriggerMessage]: Calculating... ");
Thread.sleep(500);
}
String result = future.get(); //blocking
return result;
}
private String getMeterValue(String key) throws Exception{
Future<String> future = new communicator().getMeterValueFuture(queue,centralSystem,key);
while(!future.isDone()) {
System.out.println("[getMeterValue]: Calculating...");
Thread.sleep(500);
}
String result = future.get(); //blocking
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您指的是哪个主线程,此外,如果您可以使用
完整的future
s而不是普通的旧javafuture
s。main(String [] args)
线程,不可能在没有任何形式的阻塞的情况下进行。如果您没有阻止
get
,则必须在blockingqueue
实现上阻止,否则主线程刚刚结束。您需要提交一个连续任务,而
future.get
是从外部开始的。因此,您要么在任务Future
中包含此提交,要么切换到pountableFuture
或
这个想法与Swing相同,但是您将必须使用处理程序
It depends on what main thread are you referring to, plus if you can use
CompletableFuture
s instead of plain old JavaFuture
s.main(String[] args)
threadIt's not possible to do it without any form of blocking. If you are not blocking on
get
, you'll have to block on aBlockingQueue
implementation, otherwise the main thread just ends.You'd need to submit a continuation task which is not possible with
Future.get
from the outside. So either you include this submission inside the taskFuture
has been created for, or switch toCompletableFuture
or
The idea is the same as with Swing, but you'll have to use a Handler
您可以将
Future
包装到完整的future
之类的类似中,然后使用该
alterableFuture
,以异步通过其各种来处理完成。 。
和当...
方法时。You can wrap the
Future
into aCompletableFuture
like soAnd then use that
CompletableFuture
to asynchronously handle the completion via its variousthen...
andwhen...
methods.