zoukankan      html  css  js  c++  java
  • NSThread创建线程的三种方法

     1 - (IBAction)NSThreadBtnClick:(id)sender {
     2     
     3     [self threadObjectMethod];
     4     [self threadClassMethod];
     5     [self threadNSObjectMethod];
     6     
     7     
     8 }
     9 
    10 #pragma mark - 继承自NSObject的类的对象 都可以调用这个方法 只不过拿不到线程对象
    11 - (void)threadNSObjectMethod{
    12     //自动开启线程并且执行方法
    13     //下边的这个方法在NSObject (NSThreadPerformAdditions)里边即NSObject的分类
    14     [self performSelectorInBackground:@selector(NSThreadDemo:) withObject:@"NSObjectCategory"];
    15 }
    16 
    17 #pragma mark - 通过类方法创建 分离出来一个线程 不需要手动开启线程  自动开启线程并且执行方法
    18 - (void)threadClassMethod{
    19     [NSThread detachNewThreadSelector:@selector(NSThreadDemo:) toTarget:self withObject:@"classMethodThread"];
    20 }
    21 
    22 
    23 #pragma Mark- 通过对象方法来创建线程 并且需要手动启动线程
    24 - (void)threadObjectMethod{
    25     NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(NSThreadDemo:) object:@"objectMethodThread"];
    26     //手动启动线程
    27     [thread start];
    28 
    29     
    30     
    31 }
    32 
    33 
    34 - (void)NSThreadDemo:(id)obj{
    35     NSLog(@"传入参数%@",obj);
    36     NSLog(@"hello %@",[NSThread currentThread]);
    37     
    38     
    39 }

     补充部分多线程相关的读书笔记:

    参考书籍:

    Objective-C高级编程iOS与OSX多线程和内存管理

    先写到这么多,以后再更新

    如有问题,敬请指正;

    如需转载,请注明出处,谢谢!

    我会不定期分享 iOS 相关技术文章
  • 相关阅读:
    winform编程设定listview选中行
    更新客户信息
    在Flutter中使用Android、iOS的原生 View
    怎么卸载nodejs?
    JavaScript实现简单的图片瀑布流插件
    通过代码重用攻击绕过现代XSS防御
    炫酷的播放粒子效果,你也可以学会!使用Web动画API制作
    14行实现js原生语法前端模板引擎
    什么是ESLint?
    js中require和import的区别
  • 原文地址:https://www.cnblogs.com/ITCoderW/p/6184745.html
Copyright © 2011-2022 走看看