GCD的使用.

先上个demo:


@interface ViewController ()

{

    dispatch_queue_t backGroundQueue;

    //A dispatch queue is a lightweight object to which your application submits blocks for subsequent execution

//让你的应用的blocks提交到一个分发队列(一个轻量级的对象)得以顺序执行.

}

@end


static int i = 0;


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(50,50,60,30);

    [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

}


- (void)btnPressed:(UIButton *)btn

{

    backGroundQueue = dispatch_queue_create("com.run.fan.bgqueue", NULL);

    //Creates a new dispatch queue to which blocks can be submitted.

    //Blocks submitted to a serial queue are executed one at a time in FIFO order. Note, however, that blocks submitted to independent queues may be executed concurrently with respect to each other. Blocks submitted to a concurrent queue are dequeued in FIFO order but may run concurrently if resources are available to do so.

When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.

    //作用:创建分发队列,blocks在队列中按顺序(先进先出)执行,但如果一个block提交到多个队列,则有可能会并发的执行.block对分发队列有引用,所以分发队列会等到全部block运行完毕立即释放.

    //参数1作用:A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments,sample, stackshots, and crash reports. Because applications, libraries, and frameworks can all create their own dispatch queues, a reverse-DNS naming style (com.example.myqueue) is recommended. This parameter is optional and can be NULL. 标示每个分发队列,命名规则

//参数2作用:In OS X v10.7 and later, specifyDISPATCH_QUEUE_SERIAL (or NULL) to create a serial queue or specifyDISPATCH_QUEUE_CONCURRENT to create a concurrent queue. In earlier versions of OS X, you must specify NULL for this parameter. //一般传NULL

    [self process];

    NSLog(@"hello world button");

}


- (void)process

{

    dispatch_async(backGroundQueue, ^(void){

        [self runTime];}

//Submits a block for asynchronous execution on a dispatch queue and returns immediately.

//提给分发队列一个block去让其异步执行, 

//参数1 分发队列 参数2 要提交的block

}

- (void)runTime

{

    NSTimeInterval ti0 = [[NSDate date] timeIntervalSince1970];

    NSLog(@"起始时间:%lf",ti0);

for(int i = 0;i < 60000;i++)

    {

        for(int j = 0;j < 60000;j++)

        {

            ;

        }

    }

    NSTimeInterval ti1 = [[NSDate date] timeIntervalSince1970];

    i++;

    NSLog(@"结束时间:%lf",ti1);

    NSLog(@"%d",i);

}


- (void)dealloc

{

    dispatch_release(backGroundQueue);

//A dispatch object is asynchronously deallocated once all references to it are released (the reference count becomes zero). When your application no longer needs a dispatch object that it has created, it should call this function to release its interest in the object and allow its memory to be deallocated when appropriate. Note that GCD does not guarantee that a given client has the last or only reference to a given object.                   

//your application does not need to retain or release the global (main and concurrent) dispatch queues; calling this function on global dispatch queues has no effect.


    [super dealloc];

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


其实最简单的使用就只有4步而已:

1、生命全局的分发队列

2、创建分发队列

3、提交block让其执行

4、释放分发队列


获取main线程的方法:

dispatch_async(dispatch_get_main_queue(), ^(void) {});





Submits a block for asynchronous execution on a dispatch queue and returns immediately.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值