iOS学习笔记Object-CIOS 多线程

多线程----GCD的常用函数和单例模式

2016-06-14  本文已影响379人  一抹月光3053
线程间的通信
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //执行耗时的异步操作
    dispatch_async(dispatch_get_main_queue(), ^{        
        //回到主线程刷新UI
    });
});
延时执行
 [self performSelector:@selector(run:) withObject:nil afterDelay:2.0];

p 使用GCD函数

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //2秒后异执行这里的代码
    });
一次性代码
static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
        //只执行一次的代码(这里默认是线程安全的)
    });
dispatch_barrier_async (栅栏函数)
  dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
  - (void)barrier
{
    dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"----1-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"----2-----%@", [NSThread currentThread]);
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"----barrier-----%@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"----3-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"----4-----%@", [NSThread currentThread]);
    });
}
dispatch_apply()
void
dispatch_apply(size_t iterations, dispatch_queue_t queue,
  void (^block)(size_t));
iterations  执行的次数
    queue       提交到的队列
    block       执行的任务
    size_t       传当前索引
 /**
   * 快速迭代
   */
 - (void)apply
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSString *from = @"/Users/shenzhouguangda/Desktop/icon";
    NSString *to = @"/Users/shenzhouguangda/Desktop/icon1";
    
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subpaths = [mgr subpathsAtPath:from];
    
    dispatch_apply(subpaths.count, queue, ^(size_t index) {
        NSString *subpath = subpaths[index];
        NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
        NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
        // 剪切
        [mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil];
        
        NSLog(@"%@---%@", [NSThread currentThread], subpath);
    });
}
队列组
dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //执行一个耗时操作
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //执行一个耗时操作
        
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        //等到前面的异步操作都执行完毕后,回到主线程。
    });
单例模式(GCD)
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
  //.h文件
  #import <Foundation/Foundation.h>

  @interface LGJPerson : NSObject
  /** 名字 */
  @property (nonatomic, strong) NSString *name;

  + (instancetype)sharedPerson;
  @end

  //.m文件
  #import "LGJPerson.h"

  @interface LGJPerson() <NSCopying>

  @end

  @implementation LGJPerson

  static LGJPerson *_person;

  + (instancetype)allocWithZone:(struct _NSZone *)zone
  {
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
          _person = [super allocWithZone:zone];
      });
      return _person;
  }

  + (instancetype)sharedPerson
  {
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
        _person = [[self alloc] init];
      });
      return _person;
  }

  - (id)copyWithZone:(NSZone *)zone
  {
      return _person;
  }
  @end
      // .h文件
      #define LGJSingletonH(name) +          (instancetype)shared##name;

      // .m文件
      #define LGJSingletonM(name) \
      static id _instance; \
       \
      + (instancetype)allocWithZone:(struct _NSZone *)zone \
      { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [super allocWithZone:zone]; \
        }); \
        return _instance; \
    } \
     \
    + (instancetype)shared##name \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [[self alloc] init]; \
        }); \
        return _instance; \
    } \
     \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instance; \
    }
非GCD创建单例模式
  static id _instance;

  //重写allocWithZone:方法,在这里创建唯一的实例(注意线程安  全)
    + (id)allocWithZone:(struct _NSZone *)zone
    {
        @synchronized(self) {
            if (!_instance) {
                _instance = [super allocWithZone:zone];
            }
        }
        return _instance;
    }

p提供1个类方法让外界访问唯一的实例

  + (instancetype)sharedSoundTool
  {
      @synchronized(self) {
            if (!_instance) {
                _instance = [[self alloc] init];
            }
        }
        return _instance;
    }
    实现copyWithZone:方法
    - (id)copyWithZone:(struct _NSZone *)zone
    {
        return _instance;
    }
  - (id)retain { return self; }
  - (NSUInteger)retainCount { return 1; }
  - (oneway void)release {}
  - (id)autorelease { return self; }
上一篇 下一篇

猜你喜欢

热点阅读