美文网首页
iOS 多线程

iOS 多线程

作者: crazyfox | 来源:发表于2017-03-01 18:19 被阅读5次

    iOS多线程的方式

    1.NSObject分类NSObject (NSThreadPerformAdditions)里自带的

    //手动创建NSThread

    - (void)performSelector:(SEL)aSelector onThread:(NSThread*)thr withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)arrayNS_AVAILABLE(10_5,2_0);

    - (void)performSelector:(SEL)aSelector onThread:(NSThread*)thr withObject:(nullableid)arg waitUntilDone:(BOOL)waitNS_AVAILABLE(10_5,2_0);

    // equivalent to the first method with kCFRunLoopCommonModes

    //自动创建一个NSThread

    - (void)performSelectorInBackground:(SEL)aSelector withObject:(nullableid)argNS_AVAILABLE(10_5,2_0);

    2.NSThread

    比较简单,但是无法控制执行顺序并发数量

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

    thread.threadPriority = index/10.0;

    thread.name= [NSStringstringWithFormat:@"myThread%i",index];//设置线程名称

    [threadstart];

    3.NSOperation

    创建一个NSOperation对象(实际使用中创建NSInvocationOperation或者NSBlockOperation,推荐后者,方便),然后加入NSOperationQueue中执行

    //NSInvocationOperation

    NSInvocationOperation*operation = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(setImageViewImageWithInt:)object:url];

    [queueaddOperation:operation];

    //NSBlockOperation

    NSBlockOperation*blockOperation = [[NSBlockOperationalloc]init];

    [blockOperationaddExecutionBlock:^{

    [selfsetImageViewImageWithInt:url];

    }];

    [queueaddOperation:blockOperation];

    - (void)addDependency:(NSOperation*)op;//设置依赖

    - (void)removeDependency:(NSOperation*)op;//取消依赖

    4.GCD

    创建串行队列

    dispatch_queue_t queuet=dispatch_queue_create("creatQueue", DISPATCH_QUEUE_SERIAL);

    创建并行队列

    dispatch_queue_t queuex=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    或者

    dispatch_queue_tqueuex=dispatch_queue_create("creatQueue",DISPATCH_QUEUE_CONCURRENT);

    异步执行

    dispatch_async(queuex, ^{

    });

    同步执行

    dispatch_sync(queuex, ^{

    });

    dispatch_apply():重复执行某个任务,但是注意这个方法没有办法异步执行(为了不阻塞线程可以使用dispatch_async()包装一下再执行)。

    dispatch_once():单次执行一个任务,此方法中的任务只会执行一次,重复调用也没办法重复执行(单例模式中常用此方法)。

    dispatch_time():延迟一定的时间后执行。

    dispatch_barrier_async():使用此方法创建的任务首先会查看队列中有没有别的任务要执行,如果有,则会等待已有任务执行完毕再执行;同时在此方法后添加的任务必须等待此方法中任务执行后才能执行。(利用这个方法可以控制执行顺序,例如前面先加载最后一张图片的需求就可以先使用这个方法将最后一张图片加载的操作添加到队列,然后调用dispatch_async()添加其他图片加载任务)

    dispatch_group_async():实现对任务分组管理,如果一组任务全部完成可以通过dispatch_group_notify()方法获得完成通知(需要定义dispatch_group_t作为分组标识)。

    5.线程同步

    NSLock

    [lock lock];

    <#statements#>

    [lock unlock];

    @synchronized


    @synchronized(token){

    <statements>

    }

    dispatch_semaphore_t支持信号通知和信号等待

    dispatch_semaphore_tt =dispatch_semaphore_create(1);

    dispatch_semaphore_wait(t,DISPATCH_TIME_FOREVER);//等待-1

    dispatch_semaphore_signal(t);//通知+1

    NSCondition 控制线程通信

    - (void)wait;//等待

    - (BOOL)waitUntilDate:(NSDate*)limit;

    - (void)signal;//唤醒一个线程

    - (void)broadcast;//唤醒所有线程

    参考:http://www.cnblogs.com/kenshincui/p/3983982.html#NSOperation

    相关文章

      网友评论

          本文标题:iOS 多线程

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