标签:
// 创建 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil]; // 启动 [thread start];
[NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(download:) withObject:nil];
+ (NSThread *)currentThread;
+ (NSThread *)mainThread;
+ (void)sleepUntilDate:(NSDate *)date; + (void)sleepForTimeInterval:(NSTimeInterval)ti;
- (void)setName:(NSString *)n; - (NSString *)name;
@synchronized(self) { // 被锁住的代码 }
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue_name", NULL);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 执行耗时的异步操作... dispatch_async(dispatch_get_main_queue(), ^{ // 回到主线程,执行UI刷新操作 }); });
[self performSelector:@selector(download:) withObject:@"http://xxx.jpg" afterDelay:3];
// 任务放到哪个队列中执行 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); double delay = 3; // 延迟多少秒 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), queue, ^{ // 3秒后需要执行的任务 });
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // 这里面的代码,在程序运行过程中,永远只会执行1次 });
@interface HMDataTool : NSObject + (instancetype)sharedDataTool; @end @implementation HMDataTool // 用来保存唯一的单例对象 static id _instace; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super allocWithZone:zone]; }); return _instace; } + (instancetype)sharedDataTool { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [[self alloc] init]; }); return _instace; } - (id)copyWithZone:(NSZone *)zone { return _instace; } @end
@interface HMDataTool : NSObject + (instancetype)sharedDataTool; @end @implementation HMDataTool // 用来保存唯一的单例对象 static id _instace; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super allocWithZone:zone]; }); return _instace; } + (instancetype)sharedDataTool { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [[self alloc] init]; }); return _instace; } - (id)copyWithZone:(NSZone *)zone { return _instace; } - (oneway void)release { } - (id)retain { return self; } - (NSUInteger)retainCount { return 1; } - (id)autorelease { return self; } @end
[NSOperationQueue mainQueue];
* 添加到"主队列"中的操作,都会放到主线程中执行
[[NSOperationQueue alloc] init]
* 添加到"非主队列"中的操作,都会放到子线程中执行
- (void)addOperation:(NSOperation *)op; - (void)addOperationWithBlock:(void (^)(void))block;
- (NSInteger)maxConcurrentOperationCount; - (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
- (void)cancelAllOperations;
[queue setSuspended:YES];
[queue setSuspended:NO];
[operationB addDependency:operationA];
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ }];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ }]; }];
1 [self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>];
dispatch_async(dispatch_get_main_queue(), ^{ });
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
额外需注意在使用GCD同步的时候不要和主队列一起使用,否则会出现问题,例如下图这样的用法是错误的
标签:
原文地址:http://www.cnblogs.com/damiao/p/4393740.html