iOS多线程之NSThread

来源:互联网 发布:上海华师大网络教育 编辑:程序博客网 时间:2024/05/18 17:03
////  ViewController.m//  iOS多线程之NSThread////  Created by yongpengliang on 16/3/7.//  Copyright © 2016年 jerry. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //创建子线程方法1:手动创建线程,并手动启动    //线程启动后在未执行完毕前,此处NSThread对象不会释放    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];    //启动线程    [thread start];    //创建子线程方法2:从当前线程分离出一个线程,并自动启动    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];    //创建子线程方法3:    [self performSelectorInBackground:@selector(run) withObject:nil];}/** *  子线程的执行代码 */- (void) run{    NSLog(@"%@",[NSThread currentThread]);}@end

打印结果:
2016-03-07 23:05:19.076 iOS多线程之NSThread[92481:1748616] {number = 2, name = (null)}
2016-03-07 23:05:19.076 iOS多线程之NSThread[92481:1748617] {number = 3, name = (null)}
2016-03-07 23:05:19.076 iOS多线程之NSThread[92481:1748618] {number = 4, name = (null)}

0 0
原创粉丝点击