iphone进程执行队列 dispatch queue

本文深入探讨了iOS开发中dispatch queue的工作原理及应用。包括串行队列与并发队列的区别,系统如何管理dispatch queue的线程池,以及dispatch queue的引用计数机制。通过具体的代码示例展示了如何创建和使用dispatch queue。

iphone进程执行队列 dispatch queue

 

在iphone官方文档中对此说明如下:

 

 

A dispatch queue invokes blocks submitted to it serially in FIFO order. A serial queue invokes only one block at a time, but independent queues may each invoke their blocks concurrently with respect to each other.

The global concurrent queues invoke blocks in FIFO order but do not wait for their completion, allowing multiple blocks to be invoked concurrently.

The system manages a pool of threads that process dispatch queues and invoke blocks submitted to them. Conceptually, a dispatch queue may have its own thread of execution, and interaction between queues is highly asynchronous.

Dispatch queues are reference counted via calls to dispatch_retain and dispatch_release. Pending blocks submitted to a queue also hold a reference to the queue until they have finished. Once all references to a queue have been released, the queue will be deallocated by the system.

 

 

 

//定义dispatch_queue_t变量
	dispatch_queue_t aQueue,bQueue;
	//创建dispatch_queue_t对象
	aQueue=dispatch_queue_create("firstQueue", NULL);
	bQueue=dispatch_queue_create("sencondQueue",NULL);
	
	//异步调用aQueue
	dispatch_async(aQueue, ^{
		for (int i=0; i<30; i++) {
			NSLog(@"first aQueue run:%i",i);
			[NSThread sleepForTimeInterval:10];
		}
		
	});
	//异步调用bQueue
	dispatch_async(bQueue, ^{
		for (int i=0; i<30; i++) {
			NSLog(@"second aQueue run:%i",i);
			[NSThread sleepForTimeInterval:5];
		}
		
	});

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值