您的位置:首页 > 编程语言

多线程编程——创建线程的三种方法

2014-11-08 22:07 447 查看
/**

* 获取网络数据、处理大批量数据、使用到一些比较耗时的算法时,需要使用子线程处理

*/

1、创建子线程有三种方法
(1)、NSThread
(2)、NSOperationQueue
(3)、GCD

2、使用NSThread创建子线程

// 开启一个子线程,把for循环交给子线程完成(每触发一次就创建一个线程)
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(cycle) object:nil];

// 启动子线程(手动启动)
[thread start];
[thread release];

// 启动子线程,(自动启动)

[NSThread detachNewThreadSelector:@selector(cycle) toTarget:self withObject:nil];

[NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];

// 自动启动子线程 (等同上边方法)

[self performSelectorInBackground:@selector(downloadImage) withObject:nil];

3、使用NSOperationQueue创建子线程

// NSOperationQueue

// 1.NSInvocationOperation
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"参数"];

// [invocationOperation start];

// [invocationOperation release];

// 2.NSBlockOperation

NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"blockOperaction");
}];

// [blockOperation start];

// 3.NSOperationQueue(用来存放任务的队列)

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

// 添加依赖关系,当一个任务执行完后,另一个再执行(设置当block执行完成后再执行invocationoperation)
[invocationOperation addDependency:blockOperation];

// 设置最大可同时执行的任务数量

// queue.maxConcurrentOperationCount = 1;

// 添加任务到队列中(将任务添加到队列中后会自动执行,不必手动start)
[queue addOperation:invocationOperation];
[queue addOperation:blockOperation];
[invocationOperation release];

4、使用GCD创建子线程

/**

* 1.GCD: 分派队列 ,FIFO

* 2.串行队列:前一个任务执行完毕,下一个任务才开始执行

* 3.并行队列:任务在派发的过程中是有序的,但是不用等待前一个任务完成,下一个任务就可以开始

* 4.GCD中的队列分为三种,主队列,全局队列,自定义队列

*/

// 1.使用主队列实现任务的派发,串行。主队列分派的任务,永远在主线程中

dispatch_queue_t mainQueue = dispatch_get_main_queue();

// 1.1 添加任务
dispatch_async(mainQueue, ^{
NSLog(@"第一个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第二个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第三个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第四个任务,当前线程是:%@",[NSThread
currentThread]);

});

// 2.自己创建队列,串行,任务会分派在子线程中

dispatch_queue_t myQueue = dispatch_queue_create("com.lanou.myqueue", DISPATCH_QUEUE_SERIAL);//DISPATCH_QUEUE_SERIAL:串行

// 2.1添加任务
dispatch_async(myQueue, ^{
NSLog(@"第一个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第二个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第三个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第四个任务,当前线程是:%@",[NSThread
currentThread]);
});

// 3.自定义队列,并行

dispatch_queue_t myQueue = dispatch_queue_create("com.lanou.myQueue", DISPATCH_QUEUE_CONCURRENT);//DISPATCH_QUEUE_CONCURRENT:并行
dispatch_async(myQueue, ^{
NSLog(@"第一个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第二个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第三个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(myQueue, ^{
NSLog(@"第四个任务,当前线程是:%@",[NSThread
currentThread]);
});

// 4.利用系统创建并行队列

dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//DISPATCH_QUEUE_PRIORITY_DEFAULT 优先级默认
dispatch_async(queue2, ^{
NSLog(@"第一个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"第二个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"第三个任务,当前线程是:%@",[NSThread
currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"第四个任务,当前线程是:%@",[NSThread
currentThread]);
});

// 5.实例:获取网络图片数据

__block ViewController *vc = self;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{

// 5.1 获取网络图片数据

NSURL *url = [NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/6/53bfb7f60d991.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

// 5.2在主线程中显示图片数据

dispatch_async(dispatch_get_main_queue(), ^{
[vc showImage:image];
});
});

/**

* GCD提供的福利

*/

for (int i = 0; i < 10;
i++) {

static dispatch_once_t onceToken; //让代码只走一次dispatch_once_GCD.....
dispatch_once(&onceToken, ^{
NSLog(@"haha");
});
}

for (int i = 0;
i < 10; i++) { // 代码延迟五秒执行:dispatch_after_GCD......

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
NSLog(@"让代码延迟五秒执行");
});
}

// 指定代码执行若干次

dispatch_apply(4, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(size_t t) {

NSLog(@"指定代码执行若干次");
});

/**

* 1.脱离线程:当线程内部代码执行完毕后,线程自动关闭,称为脱离线程。例如:NSThread,Background

* 2.非脱离线程:当线程内部代码执行完毕后,线程不关闭,等待着继续被使用,称为非脱离线程。例如:NSOperation

*/

5、从子线程切换到主线程的两种方法
方法一:

// 5.2在主线程中显示图片数据

dispatch_async(dispatch_get_main_queue(), ^{

[vc showImage:image];

});

方法二:

// 5.回到主线程显示图片

[self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: