简体   繁体   English

当某些东西异步运行时,是否意味着它不在主线程上?

[英]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. 这是一个重要的区别,因为完全有可能异步执行某项操作,使其最终结束在主线程上(例如:dispatch_async(dispatch_get_main_queue(),^ {some block});),这意味着它不会停止其他操作线程继续执行,但阻塞了主线程。

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". 也许用“关于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. 然后,工作的两个调用相对于彼此是同步的,并且相对于aSerialQueue上的所有其他工作都是同步的,而相对于其他所有事物都是异步的。

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. 当然,大多数时候,在iOS中,您的代码将在主线程上运行。 But you can push work onto background threads (eg with the dispatch APIs), so the distinction is important. 但是您可以将工作推送到后台线程上(例如,使用dispatch API),因此区别很重要。

The main thread is where the bulk of your code gets executed, including all of the UI. 主线程是执行大部分代码(包括所有UI)的位置。 There are other threads as well, like a web thread and a networking thread, and you can create your own threads. 还有其他线程,例如Web线程和网络线程,您可以创建自己的线程。

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. 如果它是异步的,则只要线程从当前执行的操作中释放出来,它将运行。

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

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