Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for a method to be asynchronous?

What is an asynchronous method. I think I know, but I keep confusing it with parallelism. I'm not sure what the difference between an asynchronous method is and what parallelism is.

Also what is difference between using threading classes and asynchronous classes?

EDIT

Some code demonstrating the difference between async, threading and parallelism would be useful.

like image 514
Sachin Kainth Avatar asked Apr 18 '13 12:04

Sachin Kainth


People also ask

What does asynchronous method mean?

An asynchronous method runs in a thread separate from the main application thread. The processing results are fetched through another call on another thread. Asynchronous methods help optimize the execution of resources resulting in scalable application.

What makes something asynchronous?

Asynchronous in computer programming In computer programming, asynchronous operation means that a process operates independently of other processes, whereas synchronous operation means that the process runs only as a result of some other process being completed or handed off.

What is asynchronous and example?

In a nutshell, asynchronous communication is any communication that does not take place in real-time. Emails, forum comments, corporate intranet, and even Asana or Trello boards serve as examples of asynchronous communication we deal with every day.

What's meaning of asynchronous?

Definition of asynchronous 1 : not simultaneous or concurrent in time : not synchronous asynchronous sound.


2 Answers

What are asynchronous methods?

Asynchronous methods come into the discussion when we are talking about potentially lengthy operations. Typically we need such an operation to complete in order to meaningfully continue program execution, but we don't want to "pause" until the operation completes (because pausing might mean e.g. that the UI stops responding, which is clearly undesirable).

An asynchronous method is one that we call to start the lengthy operation. The method should do what it needs to start the operation and return "very quickly" so that there are no processing delays.

Async methods typically return a token that the caller can use to query if the operation has completed yet and what its result was. In some cases they take a callback (delegate) as an argument; when the operation is complete the callback is invoked to signal the caller that their results are ready and pass them back. This is a commonly used callback signature, although of course in general the callback can look like anything.

So who does actually run the lengthy operation?

I said above that an async method starts a length operation, but what does "start" mean in this context? Since the method returns immediately, where is the actual work being done?

In the general case an execution thread needs to keep watch over the process. Since it's not the thread that called the async method that pauses, who does? The answer is, a thread picked for this purpose from the managed thread pool.

What's the connection with threading?

In this context my interpretation of "threading" is simply that you explicitly spin up a thread of your own and delegate it to execute the task in question synchronously. This thread will block for a time and presumably will signal your "main" thread (which is free to continue executing) when the operation is complete.

This designated worker thread might be pulled out of the thread pool (beware: doing very lengthy processing in a thread pool thread is not recommended!) or it might be one that you started just for this purpose.

like image 64
Jon Avatar answered Oct 13 '22 00:10

Jon


First off, what is a method and what is a thread? A method is a unit of work that either (1) performs a useful side effect, like writing to a file, or (2) computes a result, like making a bitmap of a fractal. A thread is a worker that performs that work.

A method is synchronous if in order to use the method -- to get the side effect or the result -- your thread must do nothing else from the point where you request the work to be done until the point where it is finished.

A method is asynchronous if your thread tells the method that it needs the work to be done, and the method says "OK, I'll do that and I'll call you when it is finished".

Usually the way an asynchronous method does that is it makes another worker -- it grabs a thread from the pool. This is particularly true if the method needs to make heavy use of a CPU. But not always; there is no requirement that an asynchronous method spins up another thread.

Does that make sense?

like image 26
Eric Lippert Avatar answered Oct 12 '22 23:10

Eric Lippert