zoukankan      html  css  js  c++  java
  • OC多线程操作

    多线程

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

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

    1--------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];  

    }  

    2--------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;  

    }  

    3--------Grand Central Dispatch 简称(GCD)

    Serial  串行 Concurrent  并行

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

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

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

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

    网上的图

    ......下载图片

    dispatch_queue_t queue =dispatch_queue_create(NULLDISPATCH_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

  • 相关阅读:
    MD5,SHA1,SHA256,SHA512等常用加密算法
    Encoding.GetEncoding 编码列表
    Form开发小技巧
    全国分河流水系流域边界及河流数据水系及主要流域分界图
    建筑物轮廓自然保护区城市AOI水土保持水资源水文建成区生态功能分区矢量数据shptabdwgcdr
    坐标转换一些
    mysql 查询语言使用 lisoaring
    lisoaring python 字符串 编码
    全国各省乡镇街道边界行政区划矢量地图shpmapinfocad最新20101112131415 161718
    写第一篇 随笔
  • 原文地址:https://www.cnblogs.com/yangqinglong/p/5379472.html
Copyright © 2011-2022 走看看