我在 ios 中学习并发编程主题。我为多个串行调度队列做了一个示例应用程序。
-(IBAction)SerialDispatchAction:(id)sender
{
s1queue = dispatch_queue_create( "com.newtok.firstQueue" , NULL);
s2queue = dispatch_queue_create( "com.newtok.secondQueue" , NULL);
dispatch_async(s1queue, ^{
int i=0;
while(i<5)
{
printf("First Asynchronous Dispatch Queue.....\n");
sleep(1);
i++;
}
});
dispatch_async(s2queue, ^{
int i=0;
while(i<5)
{
printf("Second Asynchronous Dispatch Queue.....\n");
sleep(1);
i++;
}
});
dispatch_sync(s1queue ,^{
int i=0;
while(i<5)
{
printf("First Synchronous Dispatch Queue.....\n");
sleep(1);
i++;
}
});
dispatch_sync(s2queue ,^{
int i=0;
while(i<5)
{
printf("Second Synchronous Dispatch Queue.....\n");
sleep(1);
i++;
}
});
}
我读到,如果您正在创建多个 Dispatch 串行队列,则队列将同时运行。我得到了这样的输出......
First Asynchronous Dispatch Queue.....
Second Asynchronous Dispatch Queue.....
First Asynchronous Dispatch Queue.....
Second Asynchronous Dispatch Queue.....
First Asynchronous Dispatch Queue.....
Second Asynchronous Dispatch Queue.....
First Asynchronous Dispatch Queue.....
Second Asynchronous Dispatch Queue.....
First Asynchronous Dispatch Queue.....
Second Asynchronous Dispatch Queue.....
First Synchronous Dispatch Queue.....
First Synchronous Dispatch Queue.....
First Synchronous Dispatch Queue.....
First Synchronous Dispatch Queue.....
First Synchronous Dispatch Queue.....
Second Synchronous Dispatch Queue.....
Second Synchronous Dispatch Queue.....
Second Synchronous Dispatch Queue.....
Second Synchronous Dispatch Queue.....
Second Synchronous Dispatch Queue.....
dispatch_async 和 dispatch_sync 有什么区别?它是如何相互浸染的?