码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发之多线程

时间:2015-05-18 22:23:16      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

多线程:

 

创建一个新的线程:也可通过alloc init 创建。。

NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(taskA) object:nil];

   开始线程:[thread1 start];

   退出线程:[NSThread exit];

 

[NSThread detachNewThreadSelector:@selector(downloadNetworkData) toTarget:self withObject:nil];

          //限制:  UI线程称为主线程

    //      其他创建的线程称为工作子线程

    //      注意:  不要在子线程中直接操作UI(间接让主线程操作UI)

创建线程,它方法里面去执行主线程,, 在主线程的方法里面去更新UI界面

[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];

 

//(3)使用通知监听线程的结束

//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealThreadExit) name:NSThreadWillExitNotification object:nil];

// Exit 退出。

 

//NSOperation 的使用 (操作/行动)

    //(1)什么是NSOperation, 作用

    //  NSOperation和NSThread相似, 实现多线程的一种机制

    //  在NSThread做了更高的抽象, 加入了block, 比NSThread简单易用

    //NSOperation抽象类, 使用使用NSInvocationOperation和NSBlockOperation

    //(2)NSInvocationOperation和NSBlockOperation

NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil];

    //注意: 创建之后需要执行, 执行的时候默认是同步的

    //[invocationOperation start];

    

NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

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

        {

            NSLog(@"B = %d",i);

        }

    }];

    //[blockOperation start];

    //操作队列, 理解: 任务列表

    //注意: 任务会异步平行执行

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [queue addOperation:invocationOperation];

    [queue addOperation:blockOperation];

 

    // GCD的使用

    // Grand Central Dispatch 简写

    // 好处:  1.支持多核心

    //       2.C和block接口, 易于使用

    //       3.较晚出现, 功能强大, 推荐使用

    

   // group 任务组

    dispatch_group_t group = dispatch_group_create();

//向任务组里面添加任务  7s完成

dispatch_group_async(group,       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        for (int i=0; i<100; i++) {

            NSLog(@"A = %d",i);

            [NSThread sleepForTimeInterval:0.07];

        }

    });

    

 

//GCD最简单开启异步任务的形式    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       

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

        {

            //子线程中不能直接更新UI

            //progressView.progress += 0.01;

            dispatch_async(dispatch_get_main_queue(), ^{

                progressView.progress += 0.01;

            });

            

            NSLog(@"progress = %.2f%%",progressView.progress*100);

            

            [NSThread sleepForTimeInterval:0.1];

        }

        

        //最后显示对话框

        dispatch_async(dispatch_get_main_queue(), ^{

            UIAlertView *alertView = [[UIAlertView alloc] init];

            alertView.message = @"下载完成";

            [alertView addButtonWithTitle:@"取消"];

            [alertView show];

            

        });

        

    });

    

 

IOS开发之多线程

标签:

原文地址:http://www.cnblogs.com/taoxu/p/4513017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!