如何创建常驻线程以及waitUntilDone参数的作用
2016-10-09 20:33
316 查看
#import "ViewController.h"
@interface
ViewController ()
@property (nonatomic,
strong)NSThread *thread;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
createThread];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// waitUntilDone设置为YES,会阻塞主线程,优先在子线程中执行task2方法,task2方法执行完后主线程再继续后面的打印
// waitUntilDone设置为NO,子线程的task2方法和主线程的打印同时进行
[self
performSelector:@selector(task2)
onThread:self.thread
withObject:nil
waitUntilDone:YES];
NSLog(@"cukiy1--%@",[NSThread
currentThread]);
NSLog(@"cukiy2--%@",[NSThread
currentThread]);
NSLog(@"cukiy3--%@",[NSThread
currentThread]);
NSLog(@"cukiy4--%@",[NSThread
currentThread]);
NSLog(@"cukiy5--%@",[NSThread
currentThread]);
NSLog(@"----------");
}
- (void)createThread
{
// 创建子线程执行任务
NSThread *thread = [[NSThread
alloc] initWithTarget:self
selector:@selector(task1)
object:nil];
[thread start];
self.thread = thread;
}
- (void)task1
{
NSLog(@"task1--%@",[NSThread
currentThread]);
// 子线程对应的runloop需要自己创建并开启
// 创建子线程对应的runloop,使子线程一直存在
NSRunLoop *currentRunloop = [NSRunLoop
currentRunLoop];
//
给runloop添加一个基于port的事件(系统事件),让runloop的运行模式不为空,保证runloop不退出
[currentRunloop addPort:[NSPort
port] forMode:NSDefaultRunLoopMode];
//
开启运行循环
[currentRunloop run];
}
- (void)task2
{
// sleep(3);
NSLog(@"task2-1-%@",[NSThread
currentThread]);
NSLog(@"task2-2-%@",[NSThread
currentThread]);
NSLog(@"task2-3-%@",[NSThread
currentThread]);
NSLog(@"task2-4-%@",[NSThread
currentThread]);
}
@end
@interface
ViewController ()
@property (nonatomic,
strong)NSThread *thread;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
createThread];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// waitUntilDone设置为YES,会阻塞主线程,优先在子线程中执行task2方法,task2方法执行完后主线程再继续后面的打印
// waitUntilDone设置为NO,子线程的task2方法和主线程的打印同时进行
[self
performSelector:@selector(task2)
onThread:self.thread
withObject:nil
waitUntilDone:YES];
NSLog(@"cukiy1--%@",[NSThread
currentThread]);
NSLog(@"cukiy2--%@",[NSThread
currentThread]);
NSLog(@"cukiy3--%@",[NSThread
currentThread]);
NSLog(@"cukiy4--%@",[NSThread
currentThread]);
NSLog(@"cukiy5--%@",[NSThread
currentThread]);
NSLog(@"----------");
}
- (void)createThread
{
// 创建子线程执行任务
NSThread *thread = [[NSThread
alloc] initWithTarget:self
selector:@selector(task1)
object:nil];
[thread start];
self.thread = thread;
}
- (void)task1
{
NSLog(@"task1--%@",[NSThread
currentThread]);
// 子线程对应的runloop需要自己创建并开启
// 创建子线程对应的runloop,使子线程一直存在
NSRunLoop *currentRunloop = [NSRunLoop
currentRunLoop];
//
给runloop添加一个基于port的事件(系统事件),让runloop的运行模式不为空,保证runloop不退出
[currentRunloop addPort:[NSPort
port] forMode:NSDefaultRunLoopMode];
//
开启运行循环
[currentRunloop run];
}
- (void)task2
{
// sleep(3);
NSLog(@"task2-1-%@",[NSThread
currentThread]);
NSLog(@"task2-2-%@",[NSThread
currentThread]);
NSLog(@"task2-3-%@",[NSThread
currentThread]);
NSLog(@"task2-4-%@",[NSThread
currentThread]);
}
@end
相关文章推荐
- 创建一个pthread线程时如何传递参数
- Android--(1)Activity组件的作用,如何创建Activity,如何启动Activity,以及Activity之间跳转的示例
- VC++ 创建线程的三种方式以及如何进行选择
- ReportView如何使用构造SQL语句带参数的存储过程创建报表以及为rdlc传递参数
- SQL 创建索引的作用以及如何创建索引
- VC++ 如何创建一个线程并传递参数
- java并发编程---如何创建线程以及Thread类的使用
- performSelectorOnMainThread:withObject: waitUntilDone 参数解释
- 如何在远程线程中创建新的线程
- 如何向线程传递参数
- 如何在java的被调用函数中创建对象,并通过参数传回?
- 如何在线程中创建可以响应事件的窗体
- 线程的yield(),sleep()以及wait()的区别
- 在VC中如何创建多个线程
- 什么是web services?它有什么作用,以及它如何实现?
- 如何向线程传递参数
- 在VC中如何创建多个线程
- 如何实现利用类成员函数创建线程
- C# multithread,threadpool,ParameterizedThreadStart,传递参数给线程以及回调
- 如何在工作线程中创建窗口?