ios 线程多种创建方式
来源:互联网 发布:java验证码实现 编辑:程序博客网 时间:2024/05/14 21:05
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//创建线程的第一种方式
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"];
[thread start];
[thread release];
//创建线程的第二种方式,NSThread类方法
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"];
//创建线程的第三种方法 NSObject方法
[self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"];
//创建线程的第四种方式
NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
[oprationQueue addOperationWithBlock:^{
//这个block语句块在子线程中执行
NSLog(@"oprationQueue");
}];
[oprationQueue release];
//第五种创建线程的方式
NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的并发数
//NSOperation 相当于java中的runnable接口,继承自它的就相当一个任务
NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"];
[oprationQueue1 addOperation:invation];//将任务添加到池子里面,可以给池子添加多个任务,并且指定它的并发数
[invation release];
//第二个任务
NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"];
invation2.queuePriority = NSOperationQueuePriorityHigh;//设置线程优先级
[oprationQueue1 addOperation:invation2];
[invation2 release];
[oprationQueue1 release];
//调用主线程,用来子线程和主线程交互,最后面的那个boolean参数,如果为yes就是等这个方法执行完了在执行后面的代码;如果为no的话,就是不管这个方法执行完了没有,接着往下走
[self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
//---------------------GCD----------------------支持多核,高效率的多线程技术
//创建多线程第六种方式
dispatch_queue_t queue = dispatch_queue_create("name", NULL);
//创建一个子线程
dispatch_async(queue, ^{
// 子线程code... ..
NSLog(@"GCD多线程");
//回到主线程
dispatch_sync(dispatch_get_main_queue(), ^{//其实这个也是在子线程中执行的,只是把它放到了主线程的队列中
Boolean isMain = [NSThread isMainThread];
if (isMain) {
NSLog(@"GCD主线程");
}
});
});
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)onMain
{
Boolean b = [NSThread isMainThread];
if (b) {
NSLog(@"onMain;;%d",b);
}
}
- (void) run:(NSString*)str
{
NSLog(@"多线程运行:::%@",str);
}
- (void) run2:(NSString*)str
{
NSLog(@"多线程运行:::%@",str);
}
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//创建线程的第一种方式
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"];
[thread start];
[thread release];
//创建线程的第二种方式,NSThread类方法
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"];
//创建线程的第三种方法 NSObject方法
[self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"];
//创建线程的第四种方式
NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
[oprationQueue addOperationWithBlock:^{
//这个block语句块在子线程中执行
NSLog(@"oprationQueue");
}];
[oprationQueue release];
//第五种创建线程的方式
NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的并发数
//NSOperation 相当于java中的runnable接口,继承自它的就相当一个任务
NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"];
[oprationQueue1 addOperation:invation];//将任务添加到池子里面,可以给池子添加多个任务,并且指定它的并发数
[invation release];
//第二个任务
NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"];
invation2.queuePriority = NSOperationQueuePriorityHigh;//设置线程优先级
[oprationQueue1 addOperation:invation2];
[invation2 release];
[oprationQueue1 release];
//调用主线程,用来子线程和主线程交互,最后面的那个boolean参数,如果为yes就是等这个方法执行完了在执行后面的代码;如果为no的话,就是不管这个方法执行完了没有,接着往下走
[self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
//---------------------GCD----------------------支持多核,高效率的多线程技术
//创建多线程第六种方式
dispatch_queue_t queue = dispatch_queue_create("name", NULL);
//创建一个子线程
dispatch_async(queue, ^{
// 子线程code... ..
NSLog(@"GCD多线程");
//回到主线程
dispatch_sync(dispatch_get_main_queue(), ^{//其实这个也是在子线程中执行的,只是把它放到了主线程的队列中
Boolean isMain = [NSThread isMainThread];
if (isMain) {
NSLog(@"GCD主线程");
}
});
});
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)onMain
{
Boolean b = [NSThread isMainThread];
if (b) {
NSLog(@"onMain;;%d",b);
}
}
- (void) run:(NSString*)str
{
NSLog(@"多线程运行:::%@",str);
}
- (void) run2:(NSString*)str
{
NSLog(@"多线程运行:::%@",str);
}
- ios 线程多种创建方式
- iOS 创建线程的多种方法
- iOS创建线程的三种方式
- iOS创建线程的三种方式
- 线程同步的多种方式
- Java基础:多线程(1)--线程的概述、创建线程的方式、线程的多种状态、线程常用的方法
- python创建字典多种方式
- 创建DataFrame有多种方式:
- 对象的多种创建方式
- python创建字典多种方式
- python-多种字典创建方式
- iOS 【Multithreading-创建线程的方式/线程状态(了解)】
- ios-day11-02(控制器的多种创建方式。init方法创建、通过storyboard的创建、通过xib创建)
- IOS界面UI设计7之UIWindown、多种方式创建视图控制器、View
- iOS GCD三种创建线程的方式
- iOS项目经验-创建线程的三种方式
- ios线程篇:多线程的几种创建方式
- C++11启动线程的多种方式
- python学习笔记(不断更新...)
- Populating Next Right Pointers in Each Node
- stay hungry,stay foolish
- 用Python登陆人人网并发状态(在已有基础上进行了修改)
- makefile的详细介绍
- ios 线程多种创建方式
- java web中将中文乱码转化的方法
- 编译器基本过程
- 基于visual Studio2013解决C语言竞赛题之1051数的顺序
- Gridview中几个Button的应用
- 一个20年web架构师的20条编程经验
- 向量点乘与叉乘
- 用VC++6.0编译调试汇编程序
- MySQL修改root密码的各种方法整理