多线程

来源:互联网 发布:中国网络为什么要设墙 编辑:程序博客网 时间:2024/04/29 21:11

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    

    self.imageView = [[UIImageViewalloc] init];

    self.imageView.frame =CGRectMake(20,520, 335,137);

    //显示模式

    self.imageView.contentMode =UIViewContentModeScaleAspectFit;

    self.imageView.backgroundColor = [UIColorgreenColor];

    [self.viewaddSubview:self.imageView];

    [_imageViewrelease];

    


    

    

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.backgroundColor = [UIColorcyanColor];

    button.frame =CGRectMake(20,40, 335,60);

    [button setTitle:@"模拟主线程"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    

    

    UIButton *buttona = [UIButtonbuttonWithType:UIButtonTypeSystem];

    buttona.backgroundColor = [UIColormagentaColor];

    [buttona setTitle:@"NSObject自带的方法实现子线程"forState:UIControlStateNormal];

    buttona.frame =CGRectMake(20,120, 335,60);

    [buttona addTarget:selfaction:@selector(buttonaAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:buttona];

    

    UIButton *buttonb = [UIButtonbuttonWithType:UIButtonTypeSystem];

    buttonb.backgroundColor = [UIColorgreenColor];

    buttonb.frame =CGRectMake(20,200, 335,60);

    [buttonb setTitle:@"使用NSThread类实现子线程"forState:UIControlStateNormal];

    [buttonb addTarget:selfaction:@selector(buttonbAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:buttonb];

    

    UIButton *buttonc = [UIButtonbuttonWithType:UIButtonTypeSystem];

    buttonc.backgroundColor = [UIColoryellowColor];

    buttonc.frame =CGRectMake(20,280, 335,60);

    [buttonc setTitle:@"测试NSOperation任务类"forState:UIControlStateNormal];

    [buttonc addTarget:selfaction:@selector(nsoperationAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:buttonc];

    

    UIButton *buttond = [UIButtonbuttonWithType:UIButtonTypeSystem];

    buttond.backgroundColor = [UIColorredColor];

    buttond.frame =CGRectMake(20,360, 335,60);

    [buttond setTitle:@"使用operationQueue实现多线程"forState:UIControlStateNormal];

    [buttond addTarget:selfaction:@selector(buttondAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:buttond];


    

    UIButton *buttone = [UIButtonbuttonWithType:UIButtonTypeSystem];

    buttone.backgroundColor = [UIColorblueColor];

    buttone.frame =CGRectMake(20,440, 335,60);

    [buttone setTitle:@"GCD系统实现多线程"forState:UIControlStateNormal];

    [buttone addTarget:selfaction:@selector(GCDAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:buttone];

    

    


    

    

    

    

}

- (void)buttonAction:(UIButton *)button

{

    NSLog(@"主线程");

    

    

    //NSTread 提供了很多跟线程操作有关的方法

    //获取当前线程

    NSThread * currentThread = [NSThreadcurrentThread];

    NSLog(@"当前线程: %@", currentThread);

    

    //获取主线程 (主线程就是单例)

    NSThread *mainThread = [NSThreadmainThread];

    NSLog(@"主线程: %@", mainThread);

    

    //让当前的线程休眠2

    [NSThread sleepForTimeInterval:2];

    

    NSInteger n =0;

    for (NSInteger i =0; i < 600000000; i++) {

        n++;

    }

    

    NSLog(@"%ld", n);

    

    

    

}

- (void)buttonaAction:(UIButton *)button

{

    NSLog(@"利用NSObject方法实现子线程");

    

    //缺点:不能处理线程的安全问题

    //优点:使用简单

    

    [selfperformSelectorInBackground:@selector(buttonAction:)withObject:nil];

}

- (void)buttonbAction:(UIButton *)button

{

    NSLog(@"使用NSThread类实现子线程");

    

    

    //优点:提供了很多针对线程的方法, 使用简单,轻量级的

    //缺点:有关于这个线程对象的所有的设置 (包括线程安全),都是开发者来进行.

    

    //NSThread 本身就是一个线程

    NSThread *thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(buttonAction:)object:nil];

    thread.name =@"淫笑然";

    //开始让线程执行任务

    [thread start];

    [thread release];

    

}

- (void)nsoperationAction:(UIButton *)button

{

    

    

    //NSOperation是一个任务类,本身并不能实现多线程操作, 直接start会在当前线程执行main方法中的代码

    

    //NSOperation是一个抽象类,要使用必须要创建一个子类, 在子类中重新main方法

    

    //NSOperation 的子类(系统提供的两个子类)

    //NSInvocationOperation

    //NSBlockOperation

    MyOpreretion *operation = [[MyOpreretionalloc] init];

    

    [operation start];

    

    [operation release];

    

    

}

- (void)buttondAction:(UIButton *)button

{

    //NSOperationQueue的使用

    NSOperationQueue *queue = [[NSOperationQueuealloc] init];

    

    //设置队列的最大并发数

    [queue setMaxConcurrentOperationCount:2];

    

    MyOpreretion *p1 = [[MyOpreretion alloc]init];

    MyOpreretion *p2 = [[MyOpreretionalloc] init];

    MyOpreretion *p3 = [[MyOpreretionalloc] init];

    MyOpreretion *p4 = [[MyOpreretionalloc] init];

    

    

    //加任务(调动线程的时候是并行的)

    [queue addOperation: p1];

    [queue addOperation: p2];

    [queue addOperation: p3];

    [queue addOperation: p4];

    

    

    [p1 release];

    [p2 release];

    [p3 release];

    [p4 release];

    [queue release];

    

    

    

    

}

- (void)GCDAction:(UIButton *)button

{

    

    //创建一个队列

    

    //参数1:给这个队列起个名字

    //参数2:队列类型

    //DISPATCH_QUEUE_CONCURRENT并行

    //DISPATCH_QUEUE_SERUAL    串行

    //dispatch_queue_t myQueue = dispatch_queue_create("尹啸然的queue", DISPATCH_QUEUE_CONCURRENT);

    


    

    //向队列中添加任务

    //不管是系统的队列,还是你的队列添加任务都用下边的

    //参数1:向哪个队列中添加任务

    //dispatch_async(myQueue, ^{

        //添加的任务是第一个的任务

       // [self buttonAction:nil];

    //});

    

    

    

    

    // 获取系统的队列

    

    // 系统提供了5个队列

    // 一个串行队列 ,叫主队列, 主要负责刷新UI界面,包含主线程

    // 四个并行队列,叫全局队列, 主要负责数据读取,文件读写等(也可以处理UI界面,但是优先级很低)

    //获取主队列

    dispatch_queue_t mainQueue =dispatch_get_main_queue();

    //之后添加队列


    //获取全局队列

    dispatch_queue_t globalQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    //异步分发任务

    dispatch_async(mainQueue, ^{

        NSLog(@"在主线程执行任务");//还是会卡死界面

    });

    dispatch_async(globalQueue, ^{

        NSLog(@"再全局队列执行任务");

    });

    

    

    //GCD的高级用法:

    //

    dispatch_async(globalQueue, ^{

        //在全局队列(子线程),获取数据, 处理数据

        NSString *str = [@"http://lady.hnr.cn/jfjs/201207/W020120720600862416176.jpg"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *url = [NSURLURLWithString:str];


        //在子线程中,可以利用同步方法获取数据

        NSData *data = [NSDatadataWithContentsOfURL:url];

        

        UIImage *image = [UIImageimageWithData:data];

        

        //数据加载完成回到主线程把数据显示到view

        

        dispatch_async(mainQueue, ^{

            self.imageView.image = image;

        });

        

        

    });

    

    

    

    //让一段代码只执行一次

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"只执行一次的代码");

    });

    

    //让一段代码延时执行

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"延迟几秒执行");

    });

    

}

0 0