C#异步Java的当量/等待? [英] Java Equivalent of C# async/await?

查看:99
本文介绍了C#异步Java的当量/等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个普通的C#开发人员,但偶尔我开发Java应用程序。我期待是有任何Java相当于C#异步电动机/ AWAIT?简单地说什么是Java等同的,

 异步任务< INT> AccessTheWebAsync()
{
    HttpClient的客户端=新的HttpClient();
    VAR urlContents =等待client.GetStringAsync(http://msdn.microsoft.com);
    返回urlContents.Length;
}


解决方案

没有,没有异步的任何当量/等待在Java中 - 甚至在之前V5 C#

这是一个相当复杂的语言功能来构建一个状态机幕后。

有相对较少的语言的在Java异步/并发性,但的java.util.concurrent 包支持包含了很多有用的的解决这个问题。 (不太等同于任务并行库,但最接近它。)

I am a normal C# developer but occasionally I develop application in java. I am looking is there is any Java equivalent of C# async/await? In simple words what is the java equivalent of,

async Task<int> AccessTheWebAsync()
{ 
    HttpClient client = new HttpClient();
    var urlContents = await client.GetStringAsync("http://msdn.microsoft.com");
    return urlContents.Length;
}

解决方案

No, there isn't any equivalent of async/await in Java - or even in C# before v5.

It's a fairly complex language feature to build a state machine behind the scenes.

There's relatively little language support for asynchrony/concurrency in Java, but the java.util.concurrent package contains a lot of useful classes around this. (Not quite equivalent to the Task Parallel Library, but the closest approximation to it.)

这篇关于C#异步Java的当量/等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆