简体   繁体   English

使用Java中的httpRequests发送asynch / await .NET 4.5吗?

[英]Sending asynch/await .NET 4.5 like httpRequests in Java?

Is there a java equivalent of the following asynch/await .NET 4.5 code to handle httprequests or really any invoked method call)? 是否存在与下面的asynch / await .NET 4.5代码等效的Java来处理httprequests或实际上是任何调用的方法调用)?

public async Task<System.IO.TextReader> DoRequestAsync(string url)
{
    HttpWebRequest req = HttpWebRequest.CreateHttp(url);
    req.AllowReadStreamBuffering = true;
    var tr = await DoRequestAsync(req);  // <- Wait here and even do some work if you want.
    doWorkWhilewaiting();                // ...look ma' no callbacks.
    return tr;
}

I was planning on calling this within a controller /GET method (to get data from 3rd partyl REST endpoint) and I'm completely new to the java world. 我打算在控制器/ GET方法中调用此方法(以从3rd partyl REST端点获取数据),而我对Java领域是一个全新的人。

Any information is greatly appreciated. 任何信息都将不胜感激。

No, Java doesn't have anything like async/await. 不,Java没有异步/等待之类的东西。 The java.util.concurrent package contains various helpful classes around concurrency (for thread pools, producer/consumer queues etc) but really it's the language support in C# 5 that ties everything together... and that's just not present in Java yet. java.util.concurrent包包含各种与并发有关的有用的类(用于线程池,生产者/消费者队列等),但实际上,C#5中的语言支持将所有内容联系在一起……而Java中尚不存在。

It's not part of the plans for Java 8 either, as far as I can tell - although things like method literals and lambda expressions will at least make the explicit callback approach a lot simpler than it is in Java 7. 据我所知,这也不是Java 8计划的一部分-尽管方法文字和lambda表达式之类的内容至少会使显式回调方法比Java 7简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM