user960567 user960567 - 2 years ago 448
Java Question

Java Equivalent of C# async/await?

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("");
return urlContents.Length;

Answer Source

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.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download