iOS开发多线程篇—创建线程
2016-06-06 12:38
357 查看
iOS开发多线程篇—创建线程
一、创建和启动线程简单说明
一个NSThread对象就代表一条线程
创建、启动线程
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 线程一启动,就会在线程thread中执行self的run方法
主线程相关用法
+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程
其他用法
获得当前线程
NSThread *current = [NSThread currentThread];
线程的调度优先级:调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
设置线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他创建线程的方式
(2)创建线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式创建并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述2种创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置
二、代码示例
1.使用古老的方式创建
实现效果:
打印结果:
2.使用NSThread创建线程
调用线程1,打印结果为:
调用线程2
调用线程3
一、创建和启动线程简单说明
一个NSThread对象就代表一条线程
创建、启动线程
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 线程一启动,就会在线程thread中执行self的run方法
主线程相关用法
+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程
其他用法
获得当前线程
NSThread *current = [NSThread currentThread];
线程的调度优先级:调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
设置线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他创建线程的方式
(2)创建线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式创建并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述2种创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置
二、代码示例
1.使用古老的方式创建
// // YYViewController.m // // // Created by apple on 14-6-23. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import <pthread.h> @interface YYViewController () - (IBAction)btnClick; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; } //按钮的点击事件 - (IBAction)btnClick { //1.获取当前线程 NSThread *current=[NSThread currentThread]; //主线程 NSLog(@"btnClick----%@",current); //2.使用for循环执行一些耗时操作 pthread_t thread; pthread_create(&thread, NULL, run, NULL); } //c语言函数 void *run(void *data) { //获取当前线程,是新创建出来的线程 NSThread *current=[NSThread currentThread]; for (int i=0; i<10000; i++) { NSLog(@"btnClick---%d---%@",i,current); } return NULL; } //多个线程,点击按钮执行按钮调用方法的时候,主线程没有被阻塞 @end
实现效果:
打印结果:
2.使用NSThread创建线程
// // YYViewController.m // // // Created by apple on 14-6-23. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import <pthread.h> @interface YYViewController () - (IBAction)btnClick; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; } //按钮的点击事件 - (IBAction)btnClick { //1.获取当前线程 NSThread *current=[NSThread currentThread]; //主线程 NSLog(@"btnClick----%@",current); //获取主线程的另外一种方式 NSThread *main=[NSThread mainThread]; NSLog(@"主线程-------%@",main); //2.执行一些耗时操作 [self creatNSThread]; // [self creatNSThread2]; // [self creatNSThread3]; } /** * NSThread创建线程方式1 * 1> 先创建初始化线程 * 2> start开启线程 */ -(void)creatNSThread { NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"]; //为线程设置一个名称 thread.name=@"线程A"; //开启线程 [thread start]; NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程B"]; //为线程设置一个名称 thread2.name=@"线程B"; //开启线程 [thread2 start]; } /** * NSThread创建线程方式2 *创建完线程直接(自动)启动 */ -(void)creatNSThread2 { // NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"]; [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"]; } /** * NSThread创建线程方式3 * 隐式创建线程, 并且直接(自动)启动 */ -(void)creatNSThread3 { //在后台线程中执行===在子线程中执行 [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"]; } -(void)run:(NSString *)str { //获取当前线程 NSThread *current=[NSThread currentThread]; //打印输出 for (int i=0; i<10; i++) { NSLog(@"run---%@---%@",current,str); } } @end
调用线程1,打印结果为:
调用线程2
调用线程3
相关文章推荐
- iOS开发多线程篇—多线程简单介绍
- iOS RTMP 视频直播开发笔记(1)----- 采集摄像头图像
- ios开发笔记之十一 --viewDidload的方法的基本介绍用代码设置按钮的属性
- iostat
- iOS9 3D touch 适配开发
- iOS 开发session验证的问题
- iOS之__block、__strong和__weak使用
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- Xcode 7 制作静态库.a 文件
- iOS开发之drawRect的作用和调用机制
- iOS学习之多点触碰
- iOS 获取手机 唯一标识-b
- 华硕笔记本电脑BIOS设置fancystart后黑屏
- iOS 代码命名规范 及Android 代码命名规范(2)Android
- iOS NSString+Helper
- iOS 第三方框架-SVProgressHUD的简单介绍
- 苹果开发 笔记(61)recursiveDescription 方法
- iOS Xcode编辑器 光标消失问题
- iOS 图片浏览器:PYPhotosView(支持原图和缩略图、多种手势、CocoaPods)
- 应用获取唯一标识