今天看了多线程的问题,看到多种实现多线程的方法,个人觉得多线程甚是强大,真是物尽其用啊。兴趣即起,总结了5中方法如下:
第一种方法,直接开启一个线程来运行
[self performSelectorInBackground:@selector(doSomeThing) withObject:nil];
第二种方法,直接创建一个线程,SEL参数是指这个线程需要执行的任务,使用这个方法的话,线程直接启动,但是不能控制线程什么时候关闭
[NSThread detachNewThreadSelector:@selector(doSomeThing) toTarget:self withObject:nil];
第三种方法,初始化一个线程并将线程启动,否则线程不会执行的。这种创建线程的方式是可以控制线程开始和关闭的,线程本身有对应的方法cancel
NSThread *thread = [[NSThread alloc ]initWithTarget:self selector:@selector(doSomeThing) object:nil];
// 启动线程
[thread start];
第四种方法,创建一个线程队列,并在线程队列中添加操作对象(两种,详解如下)
// 创建一个线程队列
NSOperationQueue *operationQuene = [[NSOperationQueue alloc]init];
// 创建一个操作对象,这个操作对象定义了线程需要做的任务。
// 1、NSInvocationOperation
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doSomeThing) object:nil];
// 将创建好的操作对象放进线程队列中,这样线程就可以直接启动了。
[operationQuene addOperation:operation];
// 2、NSBlockOperation
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:5];
NSThread *thread = [NSThread currentThread];
if ([thread isMainThread]) {
NSLog(@"Main Thread");
}else{
NSLog(@"Peer Thread");
}
NSLog(@"DONE");
}];
[operationQuene addOperation:blockOperation];
这里需要注意的是:不能直接用
NSOperation来定义一个操作对象
,因为
NSOperation只是一个抽象的类。我们若要使用必须继承他
.而好在苹果公司提供了继承
NSOperation的两个子类即
NSInvocationOperation和
NSBlockOperation.可见官方解释。
The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, youdo not use this class directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or NSBlockOperation) to perform the actual task.
我们可以直接使用这两个类来定义我们的操作对象,第一种是管理一个单一的封装的任务的执行,第二种则是管理一个或多个块中的并行执行。当然我们也可以自定义操作对象。
第五种方法,使用dispatch的分发方法,直接将需要操作的任务放进dispatch的Block块中
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 同步分发
dispatch_sync(quene, ^{
NSLog(@"do thing");
[NSThread sleepForTimeInterval:5];
NSThread *thread = [NSThread currentThread];
if ([thread isMainThread]) {
NSLog(@"Main Thread");
}else{
NSLog(@"Peer1 Thread");
}
NSLog(@"DONE");
});
// 异步分发
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"do anotherthing");
[NSThread sleepForTimeInterval:5];
NSThread *thread = [NSThread currentThread];
if ([thread isMainThread]) {
NSLog(@"Main Thread");
}else{
NSLog(@"Peer2 Thread");
}
NSLog(@"DONE");
});
在线程分发的方法中,牵涉到线程优先级的问题,这里有四种优先级
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND
且,他们的优先级依次降低。 最后,iOS的多线程问题是很复杂也是不容易掌握的问题,而这里只是多线程的入门,至于程序的逻辑问题还是需要仔细分析,弄清思路。并且,多线程的使用方法需要按照项目要求使用方法。