当我们打开音乐播放器听音乐的同时,又浏览网页,这两件事情同时执行要如何做到呢,就需要运用到多线程的知识.
线程:程序中运行的独立代码段,是程序真正的执行单元,每个运行的程序(即进程),至少包含一个线程,这个线程称为主线程.主线程在程序启动时被创建,用于执行main函数.IOS允许用户根据需要开辟若干子线程,子线程和主线程都是独立的运行单元,各自执行互不影响,因此能够并发执行.
多线程:拥有多个线程的程序1大量运算 for; 2数据读取 a.本地读取 b.数据库查找所有东西 c.网络请求
IOS中多线程实现种类有多种
注意:在多线程方法中需要添加自动释放池
-(void)button{
//
一.NSThread是一种轻量级的多线程,它有两种创建方法
//1
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sum) object:nil];
[thread start];
[thread release];
//2.
[NSThread detachNewThreadSelector:@selector(sum) toTarget:self withObject:nil];
//
二.NSObject中存在存在了一个最简单的后台执行方法
[self performSelectorInBackground:@selector(sum) withObject:self];
}
-(void)sum{
@autoreleasepool {
int sum = 0;
for (int i = 0; i< 6350; i++) {
sum = sum +i ;
}
NSLog(@"%d",sum);
}
[self performSelectorOnMainThread:@selector(createImage) withObject:@"sdfa" waitUntilDone:YES];
}
-(void)button{
//
三.NSOperation和NSOperationQueue
NSLog(@"main therad");
Lei *my = [[Lei alloc]init];
//[my start];//将NSOperation添加到NSOperationQueue中不能写start
Lei *you = [[Lei alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = 1;//最大并发数1,每次只执行一个线程,当最大并发数为1时,线程与线程同步执行
[queue addOperation:my];
[queue addOperation:you];
#import "Lei.h"
@implementation Lei
-(void)dealloc{
[super dealloc];
}
-(instancetype)init{
self = [super init];
if (self) {
}return self;
}
//对NSOperation来说,如果自己继承的,则需要在main函数中写子线程需要实现的代码,main函数是一旦创建对象会自动调用
-(void)main{
@autoreleasepool {
for (int i = 0; i < 1000; i++) {
NSLog(@"%d",i);
}
}
}
@end
//
四.系统多线程NSBlockOperation
NSBlockOperation * block= [NSBlockOperation blockOperationWithBlock:^{
@autoreleasepool {
for (int i = 0; i < 1000; i++) {
NSLog(@"%d",i);
}
}
}];//block里面就是多线程所要执行的方法
[queue addOperation:block];
//
五.GCD多线程,不能写自动释放池
-(void)cerateSerialGCD{
//第一步,创建一个同步的多线程队列
dispatch_queue_t queue = dispatch_queue_create("frist", DISPATCH_QUEUE_SERIAL);
//第二步,异步执行同步线程队列
dispatch_async(queue, ^{
//多线程的代码
//下载一张照片,并显示在界面上(同步的方式下载)
NSURL *url = [NSURL URLWithString:@"http://v.juhe.cn/movie/picurl?2583246"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSData *connection = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
UIImage *img = [UIImage imageWithData:connection];
//显示到界面,所有跟UI有关的内容全部都要写在主线程运行
//返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];
imgv.image = img;
[self.view addSubview:imgv];
});
});
}
-(void)createConcurrentGCD{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
NSURL *url = [NSURL URLWithString:@"http:\/\/v.juhe.cn\/movie\/picurl?2583246"];
//同步下载
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];
imgv.image = img;
[self.view addSubview:imgv];
});
});
}