多线程基础

2017-02-21  本文已影响8人  遥远不是北_

学习多线程的目的

多线程概念

同步 & 异步:

同步:

异步:

进程 & 线程

进程:

线程:

多线程

多线程 :

多线程执行原理:

多线程执行原理.png

多线程优缺点:

优点

缺点

多线程实现方案

多线程实现方案.png

pthread基本知识

pthread创建子线程步骤

#import <pthread.h>
int pthread_create(pthread_t * __restrict, const pthread_attr_t * __restrict,void *(*)(void *), void * __restrict);

参数:

返回值:

NSThread 介绍

NSThread 创建线程的三种方式:

//实例化线程对象的同时指定线程执行的方法@selector(demo:).
//需要手动开启线程.
- (void)threadDemo
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo:) object:@"alloc"];
    
    // 手动启动线程
    [thread start];
}

//分离出一个线程,并且自动开启线程执行@selector(demo:).
//无法获取到线程对象
- (void)threadDemo2
{
    [NSThread detachNewThreadSelector:@selector(demo:) toTarget:self withObject:@"detach"];
    
}
//方便 任何继承自NSObject的对象,都可以很容易的调用线程方法
//无法获取到线程对象
//自动开启线程执行@selector(demo:).
- (void)threadDemo3
{
    [self performSelectorInBackground:@selector(demo:) withObject:@"perform"];
    
}
上一篇 下一篇

猜你喜欢

热点阅读