zoukankan      html  css  js  c++  java
  • 多线程的四种创建方式--前两种不常用

    - (void)viewDidLoad {

        [super viewDidLoad];

        //1.Datach 方式,隐式创建

    //    [NSThread detachNewThreadSelector:@selector(detachMethod) toTarget:self withObject:nil];

        //2.NSObject的perform方法,隐式创建

    //    [self performSelectorInBackground:@selector(detachMethod) withObject:nil];

        //3.显示创建

    //    NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];

        //给线程name赋值

    //    thread.name =@"thread_name";

        //在线程创建之前设置栈空间才有效

    //    thread.stackSize =100;

         //线程全局数据 readOnly

    //    [thread.threadDictionary setObject:@"value1" forKey:@"key1"];

        //开启线程

    //    [thread start];

        方法4需要子类化一个子类

        - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument{

        

            if (self =[super initWithTarget:target selector:selector object:argument]) {

              _target =target;

              _sel =selector;

            

            }

            return self;

          }

    - (void)main{

        if (_target) {

            //父类的main方法中,_target 会掉用_sel

            [_target performSelector:_sel withObject:nil];

        }

    }

        //4.子类NSThread ,重写main方法

        Thread *thread1 =[[Thread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];

        

        [thread1 start];

    }

    - (void)detachMethod{

        //是否是主线程

        NSLog(@"%d",[NSThread isMainThread]);

        //获取当前线程,打印name

        NSLog(@"%@",[[NSThread currentThread]name]);

    }

  • 相关阅读:
    nodejs下载图片保存本地
    anaconda基本命令
    1.购买腾讯学生服务器遇到的坑
    git 命令
    JS深拷贝递归实现
    Object.prototype.toString()
    Spring核心知识点
    Spring核心知识点
    Spring核心知识点
    Spring基础知识点
  • 原文地址:https://www.cnblogs.com/yxt9322yxt/p/4805623.html
Copyright © 2011-2022 走看看