简体   繁体   中英

when something is run asynchronously does it mean that it's not on the main thread?

I am a bit confused between the concept of asynchronous loading and main thread. When something is loaded asynchronously, does it mean that it is not run on the main thread? As far as I know this is two different concept, something can be run on the main thread asynchronously and something can also be run on the background/secondary thread asynchronously. Correct me if I am wrong.

Not quite. Running asynchronously means that it doesn't stop execution from continuing on the current thread. This is an important difference because it's totally possible to do something asynchronously such that it ends up on the main thread (for example: dispatch_async(dispatch_get_main_queue(), ^{ some block });), which would mean that it's not stopping some other thread from continuing, but is blocking the main thread.

Because the main thread is so important to applications, the most common use of asynchronous code is to avoid blocking it, but it's not the only use.

(edited to add)

It's more useful, perhaps, to think of it in terms of "async with respect to x". If you do this, for example:

dispatch_async(aSerialQueue, ^{
    work();
    work();
});

Then the two invocations of work are synchronous with respect to each other, and synchronous with respect to all other work on aSerialQueue, but asynchronous with respect to everything else.

Running something asynchronously simply means that the call may return before the task is complete. This may be because it's running on a background thread. It may be because it will happen later on the current thread.

Running something synchronously doesn't mean it will happen on the main thread; it means that the call won't return until the task is complete. That may be because it happens on the same thread. It may be because it happens on another thread, but you want the current thread to wait for the other thread to complete.

Further, note that there is a difference between the main thread and the current thread. A synchronous task called on a background thread will keep that background thread from moving on until the task is complete, but won't block the main thread at all.

Of course, most of the time in iOS, your code will be running on the main thread. But you can push work onto background threads (eg with the dispatch APIs), so the distinction is important.

The main thread is where the bulk of your code gets executed, including all of the UI. There are other threads as well, like a web thread and a networking thread, and you can create your own threads.

Synchronous vs Asynchronous is a matter of when your code will be executed. You can perform a sync or async block of code on the main thread, or any other thread. If it's in sync, it will block the rest of the thread until it finishes. If it is async, it will run whenever the thread frees up from whatever it is currently performing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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