iOS多线程(2)基本使用

来源:互联网 发布:php网上订餐系统源码 编辑:程序博客网 时间:2024/05/17 02:40

2.pthread

(1)pthread的基本使用(需要包含头文件#import

 /*     第一个参数:线程对象     第二个参数:线程属性     第三个参数:void *(*)(void *) 指向函数的指针     第四个参数:传递给该函数的参数     */int pthread_create(pthread_t * __restrict, const pthread_attr_t * __restrict,        void *(*)(void *), void * __restrict);

使用举例:

 // 创建线程    pthread_t thread;    pthread_create(&thread, NULL, run, NULL);
void *run(void *param){    for (NSInteger i =0 ; i<10000; i++) {        NSLog(@"%zd--%@",i,[NSThread currentThread]);    }    return NULL;}

3.NSThread

(1)NSThread的基本使用

//第一种创建线程的方式:alloc init.//特点:需要手动开启线程,可以拿到线程对象进行详细设置    //创建线程    /*     第一个参数:目标对象     第二个参数:选择器,线程启动要调用哪个方法     第三个参数:前面方法要接收的参数(最多只能接收一个参数,没有则传nil)     */    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"wendingding"];     //启动线程    [thread start];
//第二种创建线程的方式:分离出一条子线程//特点:自动启动线程,无法对线程进行更详细的设置    /*     第一个参数:线程启动调用的方法     第二个参数:目标对象     第三个参数:传递给调用方法的参数     */    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分离出来的子线程"];
//第三种创建线程的方式:后台线程//特点:自动启动线程,无法进行更详细设置[self performSelectorInBackground:@selector(run:) withObject:@"我是后台线程"];

(2)设置线程的属性

     //设置线程的属性    //设置线程的名称    thread.name = @"线程A";    //设置线程的优先级,注意线程优先级的取值范围为0.0~1.0之间,1.0表示线程的优先级最高,如果不设置该值,那么理想状态下默认为0.5    thread.threadPriority = 1.0;

(3)线程的状态(了解)

//线程的各种状态:新建-就绪-运行-阻塞-死亡//常用的控制线程状态的方法[NSThread exit];//退出当前线程[NSThread sleepForTimeInterval:2.0];//阻塞线程[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];//阻塞线程//注意:线程死了不能复生

(4)线程间通信

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{//    [self download2];    //开启一条子线程来下载图片    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];}-(void)downloadImage{    //1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源    NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"];    //2.根据url地址下载图片数据到本地(二进制数据    NSData *data = [NSData dataWithContentsOfURL:url];    //3.把下载到本地的二进制数据转换成图片    UIImage *image = [UIImage imageWithData:data];    //4.回到主线程刷新UI    //4.1 第一种方式//    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];    //4.2 第二种方式//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];    //4.3 第三种方式    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];}

(5)线程安全

    01 前提:多个线程访问同一块资源会发生数据安全问题    02 解决方案:加互斥锁    03 相关代码:@synchronized(self){}    04 专业术语-线程同步    05 原子和非原子属性(是否对setter方法加锁)

(6)如何计算代码段的执行时间

//第一种方法    NSDate *start = [NSDate date];    //2.根据url地址下载图片数据到本地(二进制数据)    NSData *data = [NSData dataWithContentsOfURL:url];    NSDate *end = [NSDate date];    NSLog(@"第二步操作花费的时间为%f",[end timeIntervalSinceDate:start]);//第二种方法    CFTimeInterval start = CFAbsoluteTimeGetCurrent();    NSData *data = [NSData dataWithContentsOfURL:url];    CFTimeInterval end = CFAbsoluteTimeGetCurrent();    NSLog(@"第二步操作花费的时间为%f",end - start);
1 0
原创粉丝点击