多线程:NSThread 演练

//
// ViewController.m
// 03-NSThread 创建线程
//
// Created by gzxzmac on 16/1/28.
// Copyright © 2016年 gzxzmac. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self threadDemo4];
}


#pragma mark - target 参数的说明
- (void)threadDemo5 {
    Person *person = [[Person alloc]init];
    person.name = @"张三";
    person.age = 22;
    // 使用NSObject 分类来实现的创建线程,也是自动调用启动
    [person performSelectorInBackground:@selector(personInfo) withObject:nil];
}



- (void)threadDemo4 {

    Person *person = [[Person alloc]init];
    person.name = @"张三";
    person.age = 19;
    // 创建完线程之后自动会开始
    [NSThread detachNewThreadSelector:@selector(personInfo) toTarget:person withObject:@""];
}

- (void)threadDemo3 {
    Person *person = [[Person alloc]init];
    person.name = @"张三";
    person.age = 22;

    // target 值 为目标对象,不要看到就填self.要搞清楚是谁的方法。在iOS开发中,基本上是self
    // 创建线程,但是不对自动启动,需要调用start 方法之后才会启动线程
    NSThread *thread = [[NSThread alloc]initWithTarget:person selector:@selector(personInfo) object:nil];

    [thread start];
}


#pragma mark - NSThread 创建线程
- (void)threadDemo2 {
    // 使用NSObject 分类来实现的创建线程,也是自动调用启动
    [self performSelectorInBackground:@selector(demo:) withObject:@"threadDemo2"];
}


- (void)threadDemo1 {
    // 创建完线程之后自动会开始
    [NSThread detachNewThreadSelector:@selector(demo:) toTarget:self withObject:@"threadDemo1"];
}
- (void)threadDemo0 {
    // 创建线程,但是不对自动启动,需要调用start 方法之后才会启动线程
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(demo:) object:@"threadDemo0"];

    [thread start];
}

- (void)demo:(id)params {
    Person *person = [[Person alloc]init];
    person.name = @"张三";
    person.age = 22;

    [person personInfo];
    NSLog(@"%@ %@",[NSThread currentThread], params);
}

@end

你可能感兴趣的:(多线程与网络)