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.使用古老的方式创建

 1 //  2 // YYViewController.m
 3 //  4 //  5 // Created by apple on 14-6-23.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 10 #import "YYViewController.h" 11 #import <pthread.h>
12 13 14 @interface YYViewController ()
15 - (IBAction)btnClick;
16 @end 17 18 19 @implementation YYViewController
20 21 22 - (void)viewDidLoad
23 {
24  [super viewDidLoad];
25 }
26 27 28 //按钮的点击事件 29 - (IBAction)btnClick {
30 //1.获取当前线程 31 NSThread *current=[NSThread currentThread];
32 //主线程 33 NSLog(@"btnClick----%@",current);
34 35 //2.使用for循环执行一些耗时操作 36  pthread_t thread;
37 pthread_create(&thread, NULL, run, NULL);
38 }
39 40 41 //c语言函数 42 void *run(void *data)
43 {
44 //获取当前线程,是新创建出来的线程 45 NSThread *current=[NSThread currentThread];
46 47 48 for (int i=0; i<10000; i++) {
49 NSLog(@"btnClick---%d---%@",i,current);
50  }
51 return NULL;
52 }
53 54 //多个线程,点击按钮执行按钮调用方法的时候,主线程没有被阻塞 55 56 @end 57 58 


实现效果:


打印结果:


2.使用NSThread创建线程

 1 //  2 // YYViewController.m
 3 //  4 //  5 // Created by apple on 14-6-23.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYViewController.h"  10 #import <pthread.h>
 11  12  13 @interface YYViewController ()
 14 - (IBAction)btnClick;
 15 @end  16  17  18 @implementation YYViewController
 19  20 - (void)viewDidLoad
 21 {
 22  [super viewDidLoad];
 23 }
 24  25  26 //按钮的点击事件  27 - (IBAction)btnClick {
 28 //1.获取当前线程  29 NSThread *current=[NSThread currentThread];
 30 //主线程  31 NSLog(@"btnClick----%@",current);
 32  33 //获取主线程的另外一种方式  34 NSThread *main=[NSThread mainThread];
 35 NSLog(@"主线程-------%@",main);
 36  37 //2.执行一些耗时操作  38  [self creatNSThread];
 39 // [self creatNSThread2];
 40 // [self creatNSThread3];  41 }
 42  43  44 /**
 45  * NSThread创建线程方式1
 46  * 1> 先创建初始化线程
 47  * 2> start开启线程
 48 */  49 -(void)creatNSThread
 50 {
 51 NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"];
 52 //为线程设置一个名称  53 thread.name=@"线程A";
 54 //开启线程  55  [thread start];
 56  57  58 NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程B"];
 59 //为线程设置一个名称  60 thread2.name=@"线程B";
 61 //开启线程  62  [thread2 start];
 63 }
 64  65  66 /**
 67  * NSThread创建线程方式2
 68 *创建完线程直接(自动)启动
 69 */  70  71 -(void)creatNSThread2
 72 {
 73 // NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];  74  75 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];
 76 }
 77  78  79 /**
 80  * NSThread创建线程方式3
 81  * 隐式创建线程, 并且直接(自动)启动
 82 */  83  84 -(void)creatNSThread3
 85 {
 86 //在后台线程中执行===在子线程中执行  87 [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"];
 88 }
 89  90  91  92 -(void)run:(NSString *)str
 93 {
 94 //获取当前线程  95 NSThread *current=[NSThread currentThread];
 96 //打印输出  97 for (int i=0; i<10; i++) {
 98 NSLog(@"run---%@---%@",current,str);
 99  }
100 }
101 @end


调用线程1,打印结果为:


调用线程2


调用线程3

时间: 2024-09-19 09:12:40

iOS开发多线程篇—创建线程的相关文章

iOS开发多线程篇—线程安全

一.多线程的安全隐患 资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题 示例一: 示例二: 问题代码: 1 // 2 // YYViewController.m 3 // 05-线程安全 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights res

iOS开发多线程篇—线程的状态

一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 说明:创建线程有多种方式,这里不做过多的介绍. 线程的开启: [self.thread start]; 线程的运行和阻塞: (1)设置线程阻塞1,阻塞2秒 [NSThread sleepForTimeInterval:2.0]; (2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒 NSDate

iOS开发多线程篇—多线程简单介绍

一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过"活动监视器"可以查看Mac系统中所开启的进程 2.什么是线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 比如使用酷狗播放音乐.使用迅雷下载电影,都需要在线程中执行 3.线程的串行 1个线程中任务的执

iOS开发多线程篇—GCD介绍

一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为"牛逼的中枢调度器" 纯C语言,提供了非常多强大的函数 2.GCD的优势 GCD是苹果公司为多核的并行运算提出的解决方案 GCD会自动利用更多的CPU内核(比如双核.四核) GCD会自动管理线程的生命周期(创建线程.调度任务.销毁线程) 程序员只需要告诉GCD想要执行什么任务,不需要编写任何线程管理代码 3.提示 (1)GCD存在于libdispatch.dylib这个库中,这个调度库包含了GC

iOS开发多线程篇—NSOperation基本操作

一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. (3)最⼤大并发数的相关⽅方法 - (NSInteger)maxConcurrentOperationCount; - (void)setMaxConcurrentOperationCount:(NSInteger)cnt; 说明:如果没有设置最大并发数,那么并发的个数是由系统内存和CPU决定的,可能内存多久开多一点,内存少就开少一点. 注意:num

iOS开发多线程篇—线程间的通信

一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 线程间通信常用方法 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; - (void)performSelector:(SEL)aSelector onThr

iOS开发多线程篇—NSOperation简单介绍

一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation对象添加到NSOperationQueue中 (3)系统会⾃动将NSOperationQueue中的NSOperation取出来 (4)将取出的NSOperati

iOS开发多线程篇—GCD的常见用法

一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 2秒后再调用self的run方法 (2)使用GCD函数 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue()

iOS开发多线程篇—自定义NSOperation

一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // 2 // YYViewController.h 3 // 01-自定义Operation 4 // 5 // Created by apple on 14-6-26. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #i