多线程

进程:内存里的一个程序就是一个进程,运行起来的程序就是一个进程,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,每个 进程均运行在其专用且受保护的内存空间内,而这种执行的程序就称之为进程。程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程 是程序的一次执行活动,属于动态概念。

线程:线程是指进程内的一个执行单元,也是进程内的可调度实体,一个应用至少有一个线程(主线程)(IOS中UI主线程)

NSThread 

1.[NSThread detachNewThreadSelector:@selector(doSomething) toTarget:self withObject:nil];

2.NSThread* myThread = [[NSThread alloc] initWithTarget:self  selector:@selector(doSomething)  object:nil];  

   [myThread start]; 

例:下载图片

-(void)downloadImage:(NSString *) url{  

    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];  

    UIImage *image = [[UIImage alloc]initWithData:data];  

    if(image == nil){  

    }else{  

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

    }  

}  

-(void)updateUI:(UIImage*) image{  

    self.imageView.image = image;  

}  

- (void)viewDidLoad  {  

     [super viewDidLoad];  

   // [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];  

     NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];  

     [thread start];  

}  

NSOperation

使用 NSOperation的方式有两种,

一种是用定义好的两个子类:

NSInvocationOperation 和 NSBlockOperation。

另一种是继承NSOperation

在.m文件中实现main方法,main方法编写要执行的代码即可。

例:下载图片

- (void)viewDidLoad  

{  

    [super viewDidLoad];  

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self  selector:@selector(downloadImage:)   object:kURL];  

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

    [queue addOperation:operation];   

}  

-(void)downloadImage:(NSString *)url{    

    NSURL *nsUrl = [NSURL URLWithString:url];  

    NSData *data = [[NSData alloc]initWithContentsOfURL:nsUrl];  

    UIImage * image = [[UIImage alloc]initWithData:data];  

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

}  

-(void)updateUI:(UIImage*) image{  

    self.imageView.image = image;  

}  

Grand Central Dispatch 简称(GCD)

Serial  串行 Concurrent  并行

在GDC中一个操作是多线程执行还是单线程执行取决于当前队列类型和执行方法,只有队列类型为并行队列并且使用异步方法执行时才能在多个线程中执行。 

串行队列可以按顺序执行,并行队列的异步方法无法确定执行顺序。 

UI界面的更新最好采用同步方法,其他操作采用异步方法。 

GCD中多线程操作方法不需要使用@autoreleasepool,GCD会管理内存。

网上的图

例:下载图片

dispatch_queue_t queue =dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{

        //1.获取网址字符串

        NSString * urlString = @"http://www.bz55.com/uploads/allimg/121230/1-121230094954.jpg";

        //2.NSString->NSURL

        NSURL * url = [NSURL URLWithString:urlString];

        //3.同步下载

        NSData * data = [NSData dataWithContentsOfURL:url];

        UIImage * image = [UIImage imageWithData:data];

        dispatch_sync(dispatch_get_main_queue(), ^{

            self.view.backgroundColor = [UIColor colorWithPatternImage:image];

        });

    });

 

参考:

http://blog.csdn.net/totogo2010/article/details/8016129 

http://www.cnblogs.com/wendingding/p/3806821.html

http://www.jianshu.com/p/0b0d9b1f1f19

原文地址:https://www.cnblogs.com/damonWq/p/5361238.html