zoukankan      html  css  js  c++  java
  • IOS多线程之NSThread

    参考:http://blog.csdn.net/totogo2010/article/details/8010231

    1 简介

    NSThread:

    优点:NSThread 比其他两个轻量级

    缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销。

    NSThread实现的技术主要有3种:Cocoa threads,POSIX threads,Multiprocessing Services,一般使用cocoa thread 技术。

    2 使用方法

    2.1两种创建方式:

    1、[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];  

    2、NSThread* myThread = [[NSThread alloc] initWithTarget:self  

                                            selector:@selector(doSomething:)  

                                            object:nil];  

    [myThread start];  

    参数的意义:

    selector :线程执行的方法,这个selector只能有一个参数,而且不能有返回值。

    target  :selector消息发送的对象

    argument:传输给target的唯一参数,也可以是nil

    第一种方式会直接创建线程并且开始运行线程,第二种方式是先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息

    PS:不显式创建线程的方法:

    用NSObject的类方法  performSelectorInBackground:withObject: 创建一个线程:

    [Obj performSelectorInBackground:@selector(doSomething) withObject:nil];

  • 相关阅读:
    算法与数据结构(二):队列
    算法与数据结构(二):链表
    算法与数据结构(一):时间复杂度与空间复杂度
    2018总结与2019规划
    VC++ IPv6的支持
    从项目中学习HTML+CSS
    xampp 中 mysql的相关配置
    yum卸载遇到的问题--待解决
    RHEL 6.5----heartbeat
    RHEL 6.5-----MFS
  • 原文地址:https://www.cnblogs.com/qq411715078/p/5139285.html
Copyright © 2011-2022 走看看