concurrent|1|dispatch_queue_t

job:挖坑。

1、一个人挖一个坑要2hours.

2、我要挖两个坑。

 

从这里引出并行。

objc中有

dispatch object 来做多线程的事。

 

这样,又可以看看关于dispatch的那些事。

比如我要下载两个图片,就好像是上面的挖两个坑!

代码如下 :

 

 

 

     dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(myQueue, ^{
        __block UIImage *img = nil;
        dispatch_async(myQueue, ^{
            NSLog(@"testing");
        });
        dispatch_sync(myQueue, ^{

                

            NSString *url_str = [[NSString alloc] initWithString:@"http://apod.nasa.gov/apod/image/9712/orionfull_jcc_big.jpg"];
            NSData *img_data = [self downloadWithUrl:url_str];
            img = [[UIImage alloc] initWithData:img_data];

            [url_str release];
        });
        dispatch_sync(dispatch_get_main_queue(), ^{
            UIImageView *img_view = [[UIImageView alloc] initWithImage:img];

            img_view.frame = CGRectMake(00480200);
            [self.view_controller.view addSubview:img_view];
            
            [img_view release];
        });
    });
    
    dispatch_async(myQueue, ^{
        __block UIImage *img = nil;
        dispatch_sync(myQueue, ^{

             

            NSString *url_str = [[[NSString alloc] initWithString:@"http://www.dabaoku.net/fengjing/tk/tiankong/006f.jpg"] autorelease];
            NSData *img_data = [self downloadWithUrl:url_str];
            img = [[UIImage alloc] initWithData:img_data];
            
            [url_str release];

        });
        dispatch_sync(dispatch_get_main_queue(), ^{
            UIImageView *img_view = [[UIImageView alloc] initWithFrame:self.view_controller.view.bounds];
            [img_view setImage:img];
            img_view.frame = CGRectMake(100,200150150);
            [self.view_controller.view addSubview:img_view];
            [img_view release];
        });
    });
    dispatch_release(myQueue);

 

 注意到有个dispatch_get_main_queue就是通知主thread作出更新的。

 

上源代码

https://github.com/snowleung/ios_concurrent_demo 

 

posted @ 2012-06-21 00:34  snowlueng  阅读(298)  评论(0编辑  收藏  举报