多线程 NSThread(子线程)的使用

创建一个继承自"NSThread"的类并命名为"SubThread"(可以随意命名)

#import "SubThread.h"

@implementation SubThread

//复写main函数 作为入口函数
-(void)main
{
    NSLog(@"subThread");
}


@end

           回到 "ViewController.m" 文件 调用刚才创建的 "SubThread.h" 文件
#import "ViewController.h"
#import "SubThread.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //注意:显示创建需要自己管理线程 列如:开启和关闭
    //注意:线程间通信,任何对UI进行的操作一定要回到主线程
    
    
    //NSThread四种创建方式.///////
//    [self createThread];
    
    //thread配置属性
    [self configuration];
    
}

-(void)createThread
{
    //NSThread四种创建方式.///////
    //第一种显示创建
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction) object:nil];
    
    //开启子线程
    [thread1 start];
    
    //第二种 隐式创建
    //    [NSThread detachNewThreadSelector:@selector(threadAction) toTarget:self withObject:nil];
    
    //第三种 隐式创建
    //    [self performSelectorInBackground:@selector(threadAction) withObject:nil];
    
    //第四种 显示创建
    //    SubThread *subthread = [[SubThread alloc] init];
    //
    //    [subthread start];
 
}

//thread配置属性
-(void)configuration
{
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(subThreadAct) object:nil];
    
    //设置线程名字
    thread2.name = @"subThread";
    
    //设置栈空间 以kb作为单位
    thread2.stackSize = 100;
    
    //设线程的字典属性
    [thread2.threadDictionary setObject:@"AFN" forKey:@"identifier"];
    
    //设置线程优先级 范围:0.0 - 1.0 默认的线程优先级为0.5
    thread2.threadPriority = 1;
    
    //设置线程优先级 优先级为枚举值 线程启动前可以设置该属性,启动后将无法设置
    thread2.qualityOfService = NSQualityOfServiceUserInteractive;
    
    //启动线程 一般放在最后
    [thread2 start];
    
    //查看某线程的状态
    NSLog(@"executing:%d",thread2.executing);
    
    NSLog(@"finished:%d",thread2.finished);

    NSLog(@"cancelled:%d",thread2.cancelled);

}
//线程实现的方法
-(void)threadAction
{
    NSLog(@"子线程");
    
    NSLog(@"isMainThread %d",[NSThread isMainThread]);
    
    NSLog(@"isMultiThread %d",[NSThread isMultiThreaded]);
    
    NSLog(@"MainThread %@",[NSThread mainThread]);
    
    NSLog(@"currentThread %@",[NSThread currentThread]);
 
    //退出当前线程
    [NSThread exit];
    
    //取消线程的方法
    [[NSThread currentThread] cancel];
}

-(void)subThreadAct
{
    NSLog(@"%@",[NSThread currentThread]);
    
    UIImage *image = [[UIImage alloc] init];
    
    ///////-----线程间通讯的两种方式------////////
    //在子线程中回调到主线程执行任务
    [self performSelectorOnMainThread:@selector(backMainThread:) withObject:image waitUntilDone:nil];
    
    [self performSelector:@selector(backMainThread:) onThread:[NSThread mainThread] withObject:image waitUntilDone:nil modes:nil];
    
    
}

-(void)backMainThread:(UIImage *)img
{
    //获取到图片对象 在主线程中设置UIImageView的image属性
    
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,306评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,950评论 25 709
  • 1、简介:1.1 iOS有三种多线程编程的技术,分别是:1.、NSThread2、Cocoa NSOperatio...
    LuckTime阅读 5,163评论 0 1
  • #白马声慢,我自手书#生活过得浪漫有趣、把爱情过成诗,大概是每个姑娘都曾期许过的。 但这却并不是所有人都能做到的,...
    TCKEAIR阅读 1,729评论 0 0
  • 十三岁恋你时 只是偷看你 十七岁恋你时 写了六十九页纸的情书 和两首诗 十九岁恋你时 约好看遍大好山河 二十二岁恋...
    更向远行阅读 1,378评论 0 0