views:

2129

answers:

6

Asynchronous Vs. Synchronous Execution, What does it really mean?

+1  A: 

Simply said asynchronous execution is doing stuff in the background.

For example if you want to download a file from the internet you might use a synchronous function to do that but it will block your thread until the file finished downloading. This can make your application unresponsive to any user input.

Instead you could download the file in the background using asynchronous method. In this case the download function returns immediately and program execution continues normally. All the download operations are done in the background and your program will be notified when it's finished.

Michał Piaskowski
+13  A: 

When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

That being, said, in the context of computers this translates into executing a process or task on another "thread." A thread is a series of commands--a block of code--that exists as a unit of work. The operating system can manage multiple threads and assign a thread a piece ("slice") of processor time before switching to another thread to give it a turn to do some work. At its core (pardon the pun), a processor can simply execute a command--it has no concept of doing two things at one time. The operating system simulates this by allocating slices of time to different threads.

Now, if you introduce multiple cores/processors into the mix, then things CAN actually happen at the same time. The operating system can allocate time to one thread on the first processor, then allocate the same block of time to another thread on a different processor.

All of this is about allowing the operating system to manage the completion of your task while you can go on in your code and do other things. Asynchronous programming is a complicated topic because of the semantics of how things tie together when you can do them at the same time. There are numerous articles and books on the subject; have a look!

Adam Robinson
+1 Very well said.
Andrew Hare
+3  A: 

Synchronous execution means the execution happens in a single series. A->B->C->D. If you are calling those routines, A will run, then finish, then B will start, then finish, then C will start, etc.

With Asynchronous execution, you begin a routine, and let it run in the background while you start your next, then at some point, say "wait for this to finish. It's more like:

Start A->B->C->D->Wait for A to finish

The advantage is that you can execute B, C, and or D will A is still running (in the background, on a separate thread), so you can take better advantage of your resources and have less "hangs" or "waits".

Reed Copsey
+3  A: 

Synchronous means that the caller waits for the response or completion, asynchronous that the caller continues and a response comes later (if applicable).

As an example:

    static void Main(string[] args)
    {
        Console.WriteLine("Before call");
        doSomething();
        Console.WriteLine("After call");
    }

    private static void doSomething()
    {
        Console.WriteLine("In call");
    }

This will always ouput:

Before call In call After call

But if we were to make doSomething asynchronous (multiple ways to do it), then the output could become:

Before call After call In call

Because the method making the asynchronous call would immediately continue with the next line of code. I say "could", because order of execution can't be guaranteed with asynch operations. It could also execute as the original, depending on thread timings, etc.

Ragoczy
This is the most practical explanation I see here.
Steve Rowe
+11  A: 

Synchronous:

|----A-----| |-----B-----------| |-------C------|

Asynchronous:

 |----A-----|
    |-----B-----------| 
        |-------C------|
Charles Bretana
+1  A: 

I think this is bit round-about explanation but still it clarifies using real life example.

Small Example:

Lets say playing an audio involves three steps:

1. Getting the compressed song from harddisk
2. Decompress the audio.
3. Play the uncompressed audio.

If your audio player does step 1,2,3 sequentially for every song then it is synchronous. You will have to wait for some time to hear the song till the song actually gets fetched and decompressed.

If your audio player does step 1,2,3 independent of each other, then it is asynchronous. ie. While playing audio 1 ( step 3), if it fetches audio 3 from harddisk in parallel (step 1) and it decompresses the audio 2 in parallel. (step 2 ) You will end up in hearing the song without waiting much for fetch and decompress.

aJ