- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经阅读了关于 GCD and Dispatch Queue in Swift 3 的教程
但是对于同步执行和异步执行的顺序以及主队列和后台队列,我真的是一头雾水。
我知道,如果我们使用sync,那么如果我们使用async,我们就会一个接一个地执行它们strong> 那么我们可以使用QoS 来设置它们的优先级,但是这种情况呢?
func queuesWithQoS() {
let queue1 = DispatchQueue(label: "com.appcoda.myqueue1")
let queue2 = DispatchQueue(label: "com.appcoda.myqueue2")
for i in 1000..<1005 {
print(i)
}
queue1.async {
for i in 0..<5{
print(i)
}
}
queue2.sync {
for i in 100..<105{
print( i)
}
}
}
结果表明我们忽略了异步执行。我知道 queue2 应该在 queue1 之前完成,因为它是同步执行的,但是为什么我们忽略异步执行并且异步、同步和所谓的主队列之间的实际区别是什么?
最佳答案
你说:
The outcome shows that we ignore the asynchronous execution. ...
不,这只是意味着您没有给异步调度代码足够的时间来开始。
I know queue2 should be completed before queue1 since it's synchronous execution ...
首先,queue2 可能不会在 queue1 之前完成。它只是碰巧。让 queue2 做一些更慢的事情(例如循环几千次迭代而不是仅仅五次)你会看到 queue1 实际上可以与 queue2 上的内容同时运行。只需几毫秒即可开始,非常简单的 queue2 上的内容在 queue1 上的内容有机会开始之前完成。
其次,这种行为在技术上不是因为它是同步执行。只是异步需要几毫秒才能在某个工作线程上运行它的东西,而同步调用,因为我不会让你厌烦的优化,启动得更快。
but why we ignore the asynchronous execution ...
我们不会“忽略”它。只需几毫秒即可开始。
and what is the actual difference between async, sync and so-called main queue?
“异步”仅表示当前线程可以继续运行,而不是等待分派(dispatch)的代码在其他线程上运行。 “Sync”意味着当前线程应该等待分派(dispatch)的代码完成。
“主线程”是一个不同的主题,它只是指为驱动 UI 而创建的主线程。实际上,主线程是大部分代码运行的地方,基本上运行除了您手动分派(dispatch)到某个后台队列(或为您分派(dispatch)的代码,例如 URLSession
完成处理程序)之外的所有内容。
关于swift - swift 3 中调度队列的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025848/
int x = 1; System.out.println( x++ + x++ * --x ); 上面的代码打印出“5”,但我不明白怎么办?我一直为最后一个 x 取零,然后乘以仍然为 0 的第二个
我现在正在尝试使用 Preference 类 首选项 pfrOfThis = Preferences.userNodeForPackage(this) 出现错误: “类 java.util.prefs
用下面的代码 import sys print "Hello " + sys.argv[1] if len(sys.argv) > 1 else "Joe" + "." 当我运行时 python he
我的网页包含: td { padding-left:10px; } 引用的样式表包含: .rightColumn * {margin: 0; padding: 0;} 我在 rightc
使用 JPA 我有一个关于 CascadeTypes 的问题。 例如: @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST,
下面的“括号”是怎么写的? val words = List("foo", "bar", "baz") val phrase = "These are upper case: " + words ma
我只是想知道,对于以下代码,编译器是否单独使用关联性/优先级或其他一些逻辑来评估。 int i = 0, k = 0; i = k++; 如果我们根据关联性和优先级进行评估,postfix ++具有比
我设置了一个 Azure FrontDoor 服务,以主/备份类型的方式将流量分配给两个 API 管理服务。就像我希望所有流量都流向我的主要 APIM 服务一样,如果我碰巧关闭该服务(假装中断),那么
这是一个简单的 CSS: /* Smartphones (portrait and landscape) ----------- */ @media only screen and (min-devi
我设置了一个 Azure FrontDoor 服务,以主/备份类型的方式将流量分配给两个 API 管理服务。就像我希望所有流量都流向我的主要 APIM 服务一样,如果我碰巧关闭该服务(假装中断),那么
来自 Programming Perl pg 90,他说: @ary = (1, 3, sort 4, 2); print @ary; 排序右侧的逗号在排序之前求值,而左侧的逗号在排序之
+----+------------+------+ | id | title | lang | +----+------------+------+ | 1 | title 1 EN |
如何使用 Java 获取 DiffServe 代码点 (DSCP) 整数的优先级部分?我预计它涉及位移位,但由于某种原因,我似乎无法获得我期望的值。 最佳答案 假设我理解正确,只需向右执行 3 位逻辑
我有下一个运行良好的 js 函数: $(function () { $(".country").click(function () { var countries = Arra
int a[3]={10,20,30}; int* p = a; cout << *p++ << endl; 根据 wikipedia ,后缀++的优先级高于解引用,*p++应该先运行p++再解引用结
我想在优先读取归档后解决这种类型的表达式 2+3/5*9+3-4 这是我尝试解决该任务的代码我该如何解决这个问题 while ( !inputFile.eof() ) { getline( inp
我正在玩 Rhino 并注意到这种奇怪的行为似乎是运算符优先级: js> {}+{} NaN js> ''+{}+{} [object Object][object Object] js> ''+({
我想遍历文件列表并检查它们是否存在,如果文件不存在则给出错误并退出。我写了下面的代码: FILES=( file1.txt file2.txt file3.txt ) for file in ${FI
我正在执行级联 SELECT: SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 3 => If nothing found, try: SELECT * F
即将参加考试,我正在参加之前的考试。 问题: 当两个或多个样式表规则应用于同一元素时,以下哪种类型的规则将优先? 一个。任何来自浏览器的声明 b.有用户来源的正常声明 C。作者来源正常声明 d.文档级
我是一名优秀的程序员,十分优秀!