• 多线程的四种创建方式--前两种不常用


    - (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]);

    }

  • 相关阅读:
    (判断是否为弱联通分量) poj 2762
    (最大生成树) poj 1979
    (暴力) bzoj 2208
    (BFS) bzoj 1102
    (并查集) bzoj 1161
    (数学) bzoj 1800
    (博弈) bzoj 2460
    (dinic) poj 3469
    (双端队列优化的SPFA) bzoj 2100
    (判断负环) bzoj 2019
  • 原文地址:https://www.cnblogs.com/yxt9322yxt/p/4805623.html
Copyright © 2020-2023  润新知