IOS Xib 简单多线程编程代码

来源:互联网 发布:one note for mac 编辑:程序博客网 时间:2024/05/17 03:47

1、新建一个Single View Application工程,起名“MutableThread”.

2、在ViewController.xib 中拖入一个UILable,UIButton,UIActivityIndicatorView,然后和ViewController这个类进行关联。

3、源代码如下说明问题。

ViewController.h文件

#import<UIKit/UIKit.h>


@interface ViewController :UIViewController

@property (weak,nonatomic)IBOutletUILabel *myLabel;

@property (weak,nonatomic)IBOutletUIButton *myBtn;

@property (weak,nonatomic)IBOutletUIActivityIndicatorView *myActIndicView;

- (IBAction)btnClicked:(id)sender;


@end


ViewController.m文件

#import "ViewController.h"


@interface ViewController ()

- (void)firstWork;

- (void)secondWork;

- (void)thirdWork;

- (void)finallyWork;

@end


@implementation ViewController{

    int x;

    int a;

    int b;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    _myActIndicView.hidesWhenStopped =YES;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)btnClicked:(id)sender {

    //记录开始时间

    NSDate * startTime = [NSDatedate];

    [_myActIndicViewstartAnimating];

    _myBtn.alpha =0.5;

    _myBtn.enabled =false;

  

    //开始多线程 DISPATCH_QUEUE_PRIORITY_DEFAULT默认的方法是由系统决定什么时候有空才执行线程,自己不用考虑死锁的问题,GCDGreat Center Dispatch,伟大的中央调度系统)已经解决了

    //创建一个全局的多线程队列,把所有线程放到这个队列中,由系统去调度执行。在这个队列中,再取调度主线程。主线程要用 dispatch_get_main_queue()

    //多线程的代码并不是在当前这个方法中执行的,他们是两个不同的方法。是先执行当前方法,然后再去执行多线程的方法。

    dispatch_queue_t rootQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    //把有睡眠语句的方法放到队列里面执行,异步执行

    dispatch_async(rootQueue, ^{

        [self firstWork];

        dispatch_group_t subGroup = dispatch_group_create();

 //因为 secondWork thirdWork 这两个方法谁先执行,谁后执行对结果没有影响,为了提高运行效率,可以选择将这两个方法异步执行,这样的话,只需要5s,如果同步执行的话,需要7s。所以用两个dispatch_group_async,代表两个方法同时执行

        dispatch_group_async(subGroup, rootQueue, ^{

            [self secondWork];

        });

        dispatch_group_async(subGroup, rootQueue, ^{

            [self thirdWork];

        });

        //其他线程执行完后,要去通知主线程更新UI界面

        dispatch_group_notify(subGroup, rootQueue, ^{

            [self finallyWork];

            //更新主UI的方法,必须用主线程 dispatch_get_main_queue()才可以。千万不要写成rootQueue

            dispatch_async(dispatch_get_main_queue(), ^{

                [_myActIndicView stopAnimating];

                _myBtn.alpha =1;

                _myBtn.enabled =YES;

_myLabel.text = [NSStringstringWithFormat:@"the result is %d",b];

                //记录结束时间

                NSDate * endTime = [NSDatedate];

                //既然如此,NSDate * startTime这是一个局部变量,出了当前 btnClicked这个方法后,局部变量应该就已经释放了,为什么在这句中还能用呢?原因是,dispatch方法强引用了startTimestartTimeretainCount + 1,指针指向了startTime这个局部变量的内存空间。dispatch方法和btnClicked方法是两个不同的指针指向startTime这个局部变量的内存空间

                NSLog(@"use %f seconds",[endTimetimeIntervalSinceDate:startTime]);

                

            });


        });

    });

}

- (void)firstWork

{

    sleep(3);

    x ++;

    a = 5;

    b = 0;

}

- (void)secondWork

{

    sleep(5);

    b = b +x;

}

- (void)thirdWork

{

    sleep(2);

    b = b +a;

}

- (void)finallyWork

{

    NSLog(@"b is %d",b);

}


@end