社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
本文介绍多种多线程方法:xoxo_x总结
本文总结的几种多线程创建方法均为异步线程,除了最后GCD的同步线程外。
第一种:
NSThread *thread = [[NSThread alloc]initWithBlock:^{
NSLog(@"线程1");
}];
[thread start];
NSLog(@"主线程");
第二种:
[NSThread detachNewThreadWithBlock:^{
NSLog(@"线程2");
}];
NSLog(@"主线程");
第三种:
[self performSelector:@selector(doLog) withObject:self afterDelay:2];
NSLog(@"主线程");
- (void)doLog{
NSLog(@"线程3");
}
第四种:
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
NSLog(@"线程4");
}];
NSLog(@"主线程");
第五种:
NSOperationQueue *operaQueue = [[NSOperationQueue alloc]init];
NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doLog1) object:nil];
[operaQueue addOperation:op];
NSLog(@"主线程");
//maxConcurrentOperationCount 并发数量
//qualityOfService 优先级
- (void)doLog1{
NSLog(@"线程5");
}
第六种:
// ---------------GCD-----------------------
//第六种
dispatch_queue_t queue2 = dispatch_queue_create("queue", NULL);
dispatch_async(queue2, ^{
NSLog(@"线程6");
});
NSLog(@"主线程");
//同步线程
dispatch_sync(queue2, ^{
NSLog(@"线程7");
});
NSLog(@"主线程");
关于NSThread:
//获取当前线程
@property (class, readonly, strong) NSThread *currentThread;
//是否多线程
+ (BOOL)isMultiThreaded;
//创建方法 静态和动态创建
+ (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument NS_AVAILABLE(10_5, 2_0);
- (instancetype)initWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
//取消、开始
- (void)cancel NS_AVAILABLE(10_5, 2_0);
- (void)start NS_AVAILABLE(10_5, 2_0);
//获取主线程
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
关于NSOperationQueue:
//设置最大并发数量
@property NSInteger maxConcurrentOperationCount;
//设置优先级
@property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0);
//创建
- (void)addOperation:(NSOperation *)op;
- (void)addOperations:(NSArray<NSOperation *> *)ops waitUntilFinished:(BOOL)wait NS_AVAILABLE(10_6, 4_0);
- (void)addOperationWithBlock:(void (^)(void))block NS_AVAILABLE(10_6, 4_0);
关于 NSInvocationOperation:
如果说NSOperationQueue是线程队列,那么NSInvocationOperation就是一个个的队员。
//继承 : NSOperation
@interface NSInvocationOperation : NSOperation
//创建方法
- (nullable instancetype)initWithTarget:(id)target selector:(SEL)sel object:(nullable id)arg;
- (instancetype)initWithInvocation:(NSInvocation *)inv NS_DESIGNATED_INITIALIZER;
关于GCD:
//创建线程
dispatch_queue_create(const char *_Nullable label,dispatch_queue_attr_t _Nullable attr);
//创建异步线程方法
void
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
//创建同步线程
dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);
//回到主线程
dispatch_get_main_queue()
log信息:
2017-02-27 01:47:36.245 Queue[13909:903388] 主线程
2017-02-27 01:47:36.245 Queue[13909:903494] 线程1
2017-02-27 01:47:36.245 Queue[13909:903388] 主线程
2017-02-27 01:47:36.245 Queue[13909:903495] 线程2
2017-02-27 01:47:36.245 Queue[13909:903388] 主线程
2017-02-27 01:47:36.246 Queue[13909:903388] 主线程
2017-02-27 01:47:36.246 Queue[13909:903428] 线程4
2017-02-27 01:47:36.246 Queue[13909:903388] 主线程
2017-02-27 01:47:36.246 Queue[13909:903428] 线程5
2017-02-27 01:47:36.246 Queue[13909:903388] 主线程
2017-02-27 01:47:36.246 Queue[13909:903442] 线程6
2017-02-27 01:47:36.246 Queue[13909:903388] 线程7
2017-02-27 01:47:36.246 Queue[13909:903388] 主线程
2017-02-27 01:47:38.245 Queue[13909:903388] 线程3
//
// ViewController.m
// Queue
//
// Created by 冯士魁 on 2017/2/27.
// Copyright © 2017年 xoxo_x. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一种
NSThread *thread = [[NSThread alloc]initWithBlock:^{
NSLog(@"线程1");
}];
[thread start];
NSLog(@"主线程");
[thread isMainThread];//判断是否为主线程
//第二种
[NSThread detachNewThreadWithBlock:^{
NSLog(@"线程2");
}];
NSLog(@"主线程");
//第三种
[self performSelector:@selector(doLog) withObject:self afterDelay:2];
NSLog(@"主线程");
//第四种
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
NSLog(@"线程4");
}];
NSLog(@"主线程");
//第五种
NSOperationQueue *operaQueue = [[NSOperationQueue alloc]init];
NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doLog1) object:nil];
[operaQueue addOperation:op];
NSLog(@"主线程");
//maxConcurrentOperationCount 并发数量
//qualityOfService 优先级
// ---------------GCD-----------------------
//第六种
dispatch_queue_t queue2 = dispatch_queue_create("queue", NULL);
dispatch_async(queue2, ^{
NSLog(@"线程6");
});
NSLog(@"主线程");
//同步线程
dispatch_sync(queue2, ^{
NSLog(@"线程7");
});
NSLog(@"主线程");
}
- (void)doLog{
NSLog(@"线程3");
}
- (void)doLog1{
NSLog(@"线程5");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!