- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在阅读 Concurrency Programming Guide事情让我感到困惑。
我看到很多代码为任何后台任务调用以下内容:
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
现在我所说的“背景”是通俗的意思:
在主 (UI) 线程以外的任何地方执行的东西
所以按照文档,上面的语句返回任何具有不同优先级的非主线程队列。
我的问题是 - 为什么 DISPATCH_QUEUE_PRIORITY_BACKGROUND
存在?最近我还看到许多使用 DISPATCH_QUEUE_PRIORITY_BACKGROUND
的异步任务专门执行后台任务。
队列没有返回 DISPATCH_QUEUE_PRIORITY_DEFAULT
, DISPATCH_QUEUE_PRIORITY_LOW
或 DISPATCH_QUEUE_PRIORITY_HIGH
如果使用 dispatch_get_global_queue
返回它们,它们会远离主线程运行?
它们不是后台队列吗? DISPATCH_QUEUE_PRIORITY_BACKGROUND
返回的队列的具体用途是什么?服务?我已经referred to this但除了我上面提到的流行含义外,它并没有说明太多。
我敢肯定我对单词 - 后台和后台队列感到很困惑。如果有人可以解释(更好的,图形化的)- 将是一个很大的帮助。
最佳答案
这在 dispatch/queue.h header 中有很好的解释:
DISPATCH_QUEUE_PRIORITY_HIGH Items dispatched to the queue will run at high priority, i.e. the queue will be scheduled for execution before any default priority or low priority queue.
DISPATCH_QUEUE_PRIORITY_DEFAULT Items dispatched to the queue will run at the default priority, i.e. the queue will be scheduled for execution after all high priority queues have been scheduled, but before any low priority queues have been scheduled.
DISPATCH_QUEUE_PRIORITY_LOW Items dispatched to the queue will run at low priority, i.e. the queue will be scheduled for execution after all default priority and high priority queues have been scheduled.
DISPATCH_QUEUE_PRIORITY_BACKGROUND Items dispatched to the queue will run at background priority, i.e. the queue will be scheduled for execution after all higher priority queues have been scheduled and the system will run items on this queue on a thread with background status as per setpriority(2) (i.e. disk I/O is throttled and the thread's scheduling priority is set to lowest value).
请记住这是一个全局队列。其他的东西,比如系统框架,可能会被安排进去。优先级很容易耗尽 - 如果有很多 DISPATCH_QUEUE_PRIORITY_HIGH
任务被调度,默认优先级的任务可能需要等待很长时间才能执行。 DISPATCH_QUEUE_PRIORITY_BACKGROUND 中的任务可能需要等待很很长时间,因为在它们之上的所有其他优先级都必须是空的。
很多开发者滥用全局并发队列。他们想要一个执行 block ,需要一个队列,并以默认优先级使用它。这种做法可能会导致一些非常难以解决的错误。全局并发队列是一种共享资源,应该小心对待。在大多数情况下,创建私有(private)队列更有意义。
并发队列不是异步的,它是并发的。同步任务依然可以调度到里面,依然会同步执行。并发队列,如串行队列,以 FIFO 顺序出队。与串行队列不同,它们同时执行 block 。并发和异步不是一回事。
另外请记住,如果主线程空闲,并发队列可以重新使用该线程 - 事实上,它更愿意这样做而不是创建新线程。使用并发队列不会保证您不会阻塞用户界面:
Blocks submitted to these dispatch queues are invoked on a pool of threads fully managed by the system. No guarantee is made regarding which thread a block will be invoked on; however, it is guaranteed that only one block submitted to the FIFO dispatch queue will be invoked at a time.
GCD 不保证将使用哪个线程来执行并发队列上的 block 。如果使用主队列, block 将在主线程上串行执行。并发队列可以使用任何线程,并且作为优化将更喜欢使用现有线程。如果没有线程可重用,它只会创建一个新线程。事实上,主线程通常是首选(如果主线程可用于工作),因为它是“暖线程”。
重申一下:使用 Grand Central Dispatch,您可以确定任务将在主线程上执行(通过提交到主队列)。您不能确定任务不会在主线程上执行。
关于iOS GCD : Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25052629/
我正在尝试使用以下命令将 slingbox 连接设置为 1:20 类: iptables -t mangle -A to-cable -p tcp --dport 5001 -j CLASSIFY -
我使用 MailKit 作为 SMTP 客户端来发送电子邮件。 我看到有两个属性 XMessagePriority和 MessagePriority 这两者有什么区别,一个会覆盖另一个吗? 最佳答案
我有一个 5 列的布局(1 和 5 大小相同,2 和 4 大小相同): | 1 | 2 | 3 | 4 | 5 | 我们允许用户水平调整应用程序的大小(缩小它),我试图实现的行为如下:缩小第 1 列和
在 Websphere 中,可以创建“共享库”并与应用程序关联。我想知道,就类路径中的位置而言,添加共享库的位置是什么?这意味着,我想知道,在重复资源的情况下,将具有更高的“优先级”加载、共享库或 E
这个问题在这里已经有了答案: Scope of variables in "for" loop (2 个答案) New block scope for selection and iteration
我想了解优先级是如何运作的。更具体地说,设置 stub 优先级的预期输出是什么。关于此的文档有限,并且可用的文档并不能真正解释输出的样子,因此我无法验证我是否已正确实现它。 这是我当前的代码:
我正在处理优先级队列,我想检查在弹出操作期间堆属性是如何维护的。这是我的代码。 #include #include using namespace std; class g { publi
下面是典型的读写模式(读多写少) private ReadWriteLock lock = new ReentrantReadWriteLock(); private int value;
我有一个java程序是这样的 公共(public)类 PriorityQueueExample { public static void main(String[] args) { Prior
我是 java 和线程世界的新手..我只是在看下面的示例代码:- package com.alice.learnthread; class NewThread implements Runnable{
我正在研究优先级队列,我想检查如何使用可比较的类来比较这些值。这是我的代码。 #include #include using namespace std; class g { public
我正在尝试实现 Node* 的优先级队列,其中 Node 是我自己定义的一个类。我意识到拥有一个优先级的指针队列意味着它会根据地址而不是节点持有的值进行排序,所以我搜索了很多讨论论坛来找到一个解决方案
如何在 kafka 上添加延迟作业?据我了解,它不是按消息处理,而是按主题处理。我的工作有不同的时间表,我希望它们被消耗。假设一个将在接下来的 4 小时内,另一个将是我 12 月 1 日,等等。 ka
任何人都想一想。 OpenMP功能可调节cpu肌肉以处理dumbbel。在我对openmp的研究中,我们无法设置线程优先级来执行功能强大的块代码。只有一种方法(_beginthreadex或具有5.参
我有 2 个线程,其优先级已使用 setPriority() 函数设置,但它仍然显示相同的优先级? 这是代码片段: public class threadtest extends Thread {
我的数据库中有一个 photos 表,它使用 status 列来检测照片是否经过验证... 因此,如果照片的 status = 0 则其未经过验证,但如果 status = 1 则该照片已验证。 我想
我有下表用于本地化: Key | Value | lang MainTitle | Welcome to my page |
我的新老板认为你可以每年向 App Store 提交更新并将其标记为优先级一两次。如果你滥用它,你会失去你的特权吗? 他是对的。我多年来一直在提交更新,但从未听说过它? 干杯 最佳答案 您可以申请加急
当我设置 qualityOfService 时,我的 iOS 应用程序有多个线程(在 8 到 50 之间)在线程开始之前,.userInitiated线程在 .background 之前完成线程。但我
在各种浏览器中,当使用特定字体时(例如 - Helvetica Neue),如果未找到该字体,则使用直系字体中的第一个字体。因此,如果我要指定 Arial Narrow 是元素的基本字体样式,而我的读
我是一名优秀的程序员,十分优秀!