多线程

作者: lion_xion | 来源:发表于2015-10-28 19:56 被阅读55次

//创建一个请求

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:@"http://help.adobe.com/archive/en/photoshop/cs6/photoshop_reference.pdf"]];

AFHTTPRequestOperation*operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];

//指定一个文件保存的路径,放到沙盒的caches文件里

NSArray*sandBox =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

NSString*cachesPath = sandBox[0];

NSString*pdfPath = [cachesPathstringByAppendingPathComponent:@"text.pdf"];

NSLog(@"%@",pdfPath);

//把文件下载到指定的文件夹路径下,写成相应文件名

operation.outputStream= [NSOutputStreamoutputStreamToFileAtPath:pdfPathappend:NO];

//通过AF进行下载

//这是下载进度的block,里面会返回当前的下载进度,在这个block里设置hud的进度显示

[operationsetDownloadProgressBlock:^(NSUIntegerbytesRead,longlongtotalBytesRead,longlongtotalBytesExpectedToRead) {

//        hud.progress = (1.0) * (totalBytesRead) / totalBytesExpectedToRead;

}];

//当下在结束后,控制进度的hud应该消失.所以我们通过af进行下载完成的进度判断

[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {

NSLog(@"下载成功");

//        [hud removeFromSuperview];

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

NSLog(@"%@",error);

//        [hud removeFromSuperview];

}];

//把任务添加到队列里

NSOperationQueue*queue = [[NSOperationQueuealloc]init];

[queueaddOperation:operation];

}

- (void)click:(UIButton*)button{

NSIntegeri;

for( i =0; i <10000000000; i++) {

i++;

NSLog(@"%ld",i);

}

NSLog(@"%ld",i);

}

#pragma mark -第一种解决线程问题的方法,NSObject提供的方法

- (void)NSObjectThread:(UIButton*)button{

[selfperformSelectorInBackground:@selector(GCDAciton:)withObject:button];

//优点:写法特别简单,能快速开辟一个临时的线程

//缺点:不能保证线程使用时候的数据安全

}

#pragma mark -第二种方式NSThread

- (void)NSThreadAction:(UIButton*)button{

// NSThread本身就是一个线程类,他可以控制线程的休眠或者创建

//当前的主线程

NSLog(@"%@",[NSThreadcurrentThread]);

//主线程休眠3秒

[NSThreadsleepForTimeInterval:3];

NSLog(@"11111");

//如果用NSThread创建对象,创建出来的就是新的线程

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

//可以给新的线程对象起名

thread.name=@"王脏";

//子线程开始

[threadstart];

//优点:可以直接通过创建的方式来控制线程

//缺点:什么都需要手动设置,包括名,开始,太麻烦

}

#pragma mark -第三种,NSOperation任务

- (void)operationAciton:(UIButton*)button{

//NSOperation是一个抽象类,如果想使用这个抽象类不需要创建一个它的子类

myOperation*operation = [[myOperationalloc]init];

[operationstart];

//它是不能单独拿出来使用,如果单独使用和之前不考虑线程的方式是一样的会打死主线程,它一般和NSOperationQueue配合使用

}

- (void)operationQueue:(UIButton*)button{

//队列和任务一起配合使用解决多线程问题

//队列:队列中通过一个线程池来管理所有闲置的线程,这样可以提高重用利用率,避免重复的创建线程,整合资源

//优点:内部不需要关心线程的安全问题,用起来相对简单

//缺点:效率稍微有点低

NSOperationQueue*queue = [[NSOperationQueuealloc]init];

//设置对打并发数

[queuesetMaxConcurrentOperationCount:2];

//创建要执行的任务

myOperation*op1 = [[myOperationalloc]init];

myOperation*op2 = [[myOperationalloc]init];

myOperation*op3 = [[myOperationalloc]init];

myOperation*op4 = [[myOperationalloc]init];

myOperation*op5 = [[myOperationalloc]init];

//把任务加到队列里

[queueaddOperation:op1];

[queueaddOperation:op2];

[queueaddOperation:op3];

[queueaddOperation:op4];

[queueaddOperation:op5];

}

- (void)GCDAciton:(UIButton*)button{

//    // GCD是苹果提供的一种处理多线程的解决方案,整体比较好,和operationQueue两种是常见的解决多线程问题的方法

//    //这个方法可以保证无论哪个线程执行都只执行一次

//    static dispatch_once_t oneToken;

//    dispatch_once(&oneToken, ^{

//

//    });

//    //自定义一个队列

//    //第一个参数:给队列期一个名

//    //第二个参数:设置队列并行

//    // DISPATCH_QUEUE_CONCURRENT并行队列

//

//    dispatch_queue_t myQueue = dispatch_queue_create("zhangyangyang", DISPATCH_QUEUE_CONCURRENT);

//    //接下来,可以在队列里进行一些任务

//    dispatch_async(myQueue, ^{

//        //任务写在block里

//        NSInteger count = 0;

//        for (NSInteger i = 0; i < 6000000 ; i++) {

//            count++;

//        }

//        NSLog(@"%ld",count);

//    });

//网络请求一般会在子线程里进行加载,但是显示这个数据都是在主线程里进行,所以需要把请求的数据放到主线程使用

//定义一个全局队列

//第一个参数:设置当前队列优先

//第二个参数:没实际意义,留给以后用

dispatch_queue_tglobalQuene =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

//再获取一下当前的朱队列,也就是主线程

dispatch_queue_tmainQuene =dispatch_get_main_queue();

//通过异步进行数据请求

dispatch_async(globalQuene, ^{

//通过网址求求图片数据

NSString*picUrl =@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";

NSURL*url = [NSURLURLWithString:picUrl];

NSData*data = [NSDatadataWithContentsOfURL:url];

UIImage*image = [UIImageimageWithData:data];

//把请求下来的数据到主线程进行刷新

dispatch_async(mainQuene, ^{

//刷新数据

self.imageView.image= image;

});

});

}

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

      本文链接:https://www.haomeiwen.com/subject/uzaxhttx.html