串行与并发 Que && sync 与异步任务的差异



我是ios开发初学者,学习多读。如果我错了请告诉我

I study that

Serial Queue: 1 thread.

Concurrent Que:每个任务有多个线程。

同步任务:阻塞其他任务直到其完成

异步任务:任务可以并行运行。

我为并发队列同步任务和串行队列异步任务写了一个代码,如下所示:

print("Concurrent Que Sync task ")

let que1 = DispatchQueue(label: "AAA", attributes: .concurrent)
que1.sync{
print("Que1 Started")
for index in 1...3{
print("1.(index)")
}
print("Que1 Ended")
}
let que2 = DispatchQueue(label: "BBB", attributes: .concurrent)
que2.sync{
print("Que2 Started")
for index in 1...2{
print("2.(index)")
}
print("Que2 Ended")
}
print("Serial Que Async task")

let que1 = DispatchQueue(label: "AAA")
que1.async{
print("Que1 Started")
for index in 1...3{
print("1.(index)")
}
print("Que1 Ended")
}
let que2 = DispatchQueue(label: "BBB")
que2.async{
print("Que2 Started")
for index in 1...2{
print("2.(index)")
}
print("Que2 Ended")
}

我得到的输出是:

Concurrent Que Sync task 
Que1 Started
1.1
1.2
1.3
Que1 Ended
Que2 Started
2.1
2.2
Que2 Ended
Serial Que Async task
Que1 Started
Que2 Started
2.1
2.2
Que2 Ended
1.1
1.2
1.3
Que1 Ended

我只是有一个问题,在并发Que同步任务的情况下:因为我们有conc队列,那么我们将有2个线程&我们每次只能在两个线程上运行一个任务,因为它是同步的,所以在task1完成后启动task2,而不是在两个线程上并行运行。

以及

在串行队列异步任务的情况下:因为我们有串行队列,所以我们将只有1个线程&我们可以运行多个任务,但由于只有一个线程,我们必须在task1之后运行task2。那么它是如何并行运行两个任务的…

PS:如果我理解错了请纠正我

问题是您正在创建2个并发队列和2个串行队列。你应该做的是-

  1. 创建一个并发队列,并将两个任务添加到同一个并发队列
  2. 创建一个串行队列并将两个任务添加到同一个串行队列

关于同步. .现在sync要做的是,它会阻塞调用线程,直到任务完成。也就是说,一旦执行了块内的所有任务,控件将返回。假设您的代码是这样的——在第一行yourQueue中。sync {task1}第二行是yourQueue。sync {task2}. 在这里,除非第一行代码完全执行,包括{}块中的task1,否则第二行不会被调用。因此,不管队列是串行队列还是并发队列,task1将首先完全执行,task2将在其后执行。本质上是concurrentQueue。同步将产生与串行队列相同的结果。但是如果有第三行,那么只有在task1和task2完成后才会得到控制,因为它们都是同步调用



关于异步…现在,如果你在上面的例子中使用异步而不是同步,控制将在块被击中后立即返回。也就是说,如果代码是这样的——在第一行yourQueue中。async {task1}第二行是yourQueue。async {task2}. 现在,第二行将在第一行之后立即调用,而不必真正等待task1完成。如果这个yourQueue是一个串行队列,那么task1和task2将只在一个线程中执行,task2将等待task1完成。如果你的队列是并发队列,那么task1和task2将并行执行,因为它们有单独的线程分配。

我稍微修改了一下你的代码。尝试将que1和que2更改为同步和异步,并检查结果。注意"另一个任务"是如何以及何时执行的。

print("Concurrent Que Test")
let que1 = DispatchQueue(label: "AAA", attributes: .concurrent)
que1.sync{
print("Concurrent Que task1 Started")
for index in 1...3{
print("Concurrent - 1.(index)")
}
print("Concurrent Que task1 Ended")
}
que1.sync{
print("Concurrent Que task2 Started")
for index in 1...2{
print("Concurrent - 2.(index)")
}
print("Concurrent Que task2 Ended")
}
print("Another task - 1")
print("nnSerial Que Async Test")

let que2 = DispatchQueue(label: "AAA")
que2.async{
print("Serial Que task1 Started")
for index in 1...3{
print("Serial - 1.(index)")
}
print("Serial Que task1 Ended")
}
que2.async{
print("Serial Que task2 Started")
for index in 1...2{
print("Serial - 2.(index)")
}
print("Serial Que task2 Ended")
}
print("Another task - 2")

在多线程编程中有两件事需要理解:

1。串行和并发队列

队列可以是"串行"的,其中任务(或闭包)严格地一个接一个地执行。在给定的时间内只能执行一项任务。其余任务等待当前任务完成。

队列可以是"concurrent",系统可以并发地运行任务。如果系统仍然有资源,那么它从队列中取出下一个任务,并在第一个任务仍在运行时将其启动到另一个线程中执行。

2。同步和异步执行任务

创建队列后,可以使用两个函数将任务放在队列上:sync-相对于当前队列同步执行async-相对于当前队列的异步执行.

同步sync函数只在任务完成后才将控制权交还给当前队列,从而阻塞当前队列。

sync函数相比,async函数在启动任务执行后立即将控制权返回给当前队列,而不必等待任务执行完成。因此,async函数不会阻塞当前队列上代码的执行。

因此,队列的类型决定了任务之间的执行方式,sync/async函数决定了调用这些函数后何时重新获得执行代码的控制权。

print("Concurrent Que Sync task")
let que1 = DispatchQueue(label: "AAA", attributes: .concurrent)
que1.sync{
print("Que1 Started")
for index in 1...3{
print("1.(index)")
}
print("Que1 Ended")
}
// The code below will be executed only after completion of task 1, since we use a sync call even if the queue is concurrent
let que2 = DispatchQueue(label: "BBB", attributes: .concurrent)
que2.sync{
print("Que2 Started")
for index in 1...2{
print("2.(index)")
}
print("Que2 Ended")
}
print("Serial Que Async task")

let que1 = DispatchQueue(label: "AAA")
que1.async{
print("Que1 Started")
for index in 1...3{
print("1.(index)")
}
print("Que1 Ended")
}
// The code below will be executed immediately after task 1 is added to the queue, since we use an async function that returns control immediately after the call
let que2 = DispatchQueue(label: "BBB")
que2.async{
print("Que2 Started")
for index in 1...2{
print("2.(index)")
}
print("Que2 Ended")
}

最新更新