简体   繁体   English

Java 开发人员了解 C#“async/await”模式

[英]Understanding the C# “async/await” pattern by Java developer

I am coming from a Java development environment and has started to code in C#.我来自 Java 开发环境,并已开始使用 C# 进行编码。 I have noticed the 'async/await' pattern in C# that I have never seen in Java.我注意到 C# 中的“async/await”模式,我在 Java 中从未见过。 What is it exactly?究竟是什么? I have browsed internet for a while and can't find the definite explanation that would clarify my understanding of what it is.我已经浏览了一段时间的互联网,但找不到明确的解释来阐明我对它是什么的理解。

So let's define the following scenario:因此,让我们定义以下场景:

  1. Thread 'T' (eg GUI thread) is executing a GUI async function 'F'线程“T”(例如 GUI 线程)正在执行 GUI 异步函数“F”
  2. At some point in that async function 'F' we call 'await' on an “awaitable” object 'A' (most probably a Task/Task<>).在异步函数 'F' 的某个时刻,我们在“awaitable”对象 'A'(很可能是 Task/Task<>)上调用了 'await'。
  3. Then, the 'await' call is going to free/yield (but not suspend) the execution of thread 'T' (in this case the GUI thread) in order to run/execute some other Task(s) while 'awaitable' 'A' is executing its work.然后,'await' 调用将释放/放弃(但不挂起)线程 'T'(在本例中为 GUI 线程)的执行,以便在 'awaitable' 时运行/执行其他一些任务A' 正在执行其工作。
  4. When the 'awaitable' 'A' object is finished doing its work the execution of the async function 'F' resumes.当“awaitable”“A”对象完成其工作时,异步函数“F”的执行将恢复。 In the above scenario (if I described it correctly), which thread will execute 'awaitable' 'A' method?在上面的场景中(如果我描述正确的话),哪个线程将执行 'awaitable' 'A' 方法? – the GUI thread or some other thread from the pool?. – GUI 线程或池中的其他线程?。 If it is a pool thread (not the GUI thread) and I am accessing in that method GUI resources (eg buttons, labels, grid view etc), am I going to corrupt GUI thread data?如果它是一个池线程(不是 GUI 线程)并且我正在使用该方法访问 GUI 资源(例如按钮、标签、网格视图等),我会破坏 GUI 线程数据吗? Remember that I coming from Java world where there is only one GUI thread that can change/manipulate the GUI resources.请记住,我来自 Java 世界,那里只有一个 GUI 线程可以更改/操作 GUI 资源。

first off all the both syntaxes is difference.首先,这两种语法都是不同的。

Thread Syntax :-线程语法:-

Thread thread = new Thread(() => VoidMethod("","",""));

thread.Start();

https://www.c-sharpcorner.com/blogs/asynchronous-multithreaded-programming-with-example-in-c-sharp https://www.c-sharpcorner.com/blogs/asynchronous-multithreaded-programming-with-example-in-c-sharp

if your using this thread doesn't return any value.如果您使用此线程不返回任何值。 while entire request complete.当整个请求完成时。

Task/Task<> Syntax :-任务/任务<> 语法:-

public async Task<int> VoidMethod("","","")
{
   await Task.Run(() => VoidMethod("","",""));
   return 1; 
}

https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/ https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/

if your using this method returns value.如果您使用此方法返回值。 while entire request complete.当整个请求完成时。

The best place to start is the docs:最好的起点是文档:

Asynchronous programming with async and await 使用 async 和 await 进行异步编程

Than, any blog post by Stephen Toub or Stephen Cleary .比起Stephen ToubStephen Cleary 的任何博客文章。

  1. await() may cause return from the async method, but its execution is not finished, it will be called again some time later. await()可能会导致从 async 方法返回,但它的执行未完成,稍后会再次调用。 In order to be called later, it gets in the line to some event before return, just like a thread can call to Object.wait() in Java, which means the thread gets in the line to some event and leaves the processor.为了稍后被调用,它在返回之前进入某个事件的队列,就像在Java中线程可以调用Object.wait() ,这意味着线程进入某个事件的队列并离开处理器。 Async method leaves the thread, as the thread plays the role of processor for it.异步方法离开线程,因为线程为它扮演处理器的角色。

  2. when the event occur, the async method is resumed, that is, it is just invoked again on a next available thread of the thread pool - just like a thread after wait() runs on a next available processor.当事件发生时,异步方法被恢复,也就是说,它只是在线程池的下一个可用线程上再次调用 - 就像 wait() 之后的线程在下一个可用处理器上运行一样。 In order to continue execution not from the start, but from the last place where it returned because of await() , the information about the state is saved and then used in the form: switch(state) { case 0: goto 0'; case 1: goto 10'; .... }为了不是从头继续执行,而是从它因为await()返回的最后一个地方继续执行,关于状态的信息被保存,然后以以下形式使用: switch(state) { case 0: goto 0'; case 1: goto 10'; .... } switch(state) { case 0: goto 0'; case 1: goto 10'; .... }

Note this is bytecode, so goto can be used.请注意,这是字节码,因此可以使用goto To save the state, we need a field of an object, so async method in fact is not a method but a class, which is instantiated at the place where it is called first time.为了保存状态,我们需要一个对象的字段,所以 async 方法实际上不是一个方法而是一个类,它在第一次被调用的地方被实例化。

Because of direct use of goto , it is hard or sometimes impossible to code async method in plain C# or Java.由于直接使用goto ,很难或有时不可能用纯 C# 或 Java 编写异步方法。 But it is always possible to code each part of the async method as a separate method and use that methods as callbacks for the awaited events.但是始终可以将异步方法的每个部分编码为一个单独的方法,并将该方法用作等待事件的回调。 Async/await is just another syntax for asynchronous programming, along with callbacks, actors, ComplatableFuture, java.util.concurrent.Flow etc. Async/await 只是异步编程的另一种语法,还有回调、actor、ComplatableFuture、java.util.concurrent.Flow 等。

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

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