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

  • 相关阅读:
    博客园美化-SimpleMemor
    Java多线程-synchronized与ReentrantLock
    springboot中删除@SessionAttributes注解的属性
    SSM整合笔记
    Spring中xml和注解方式使用AOP
    Mysql 数据库基本操作
    Mysql 二进制包安装
    named piped tcp proxy 下载
    docker容器中日志文件过大处理方法
    自动做bond的脚本
  • 原文地址:https://www.cnblogs.com/qq411715078/p/5139285.html
Copyright © 2011-2022 走看看