NSThread-4

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self createThread1];

}

//第一种方法

-(void)createThread1{

//创建线程

//target :selector消息发送的对象

//selector:线程执行的方法,这个selector最多只能接收一个参数

//object : 传给selector的唯一参数,也可以是nil

NSThread *thread = [[NSThread alloc]initWithTarget:self  selector:@selector(run:) object:@"jack"];

//启动线程

[thread start];

}

//创建线程第二种方法

- (void)createThread2{

// 不能获得当前的线程

[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:"rose"];

}

//第三种方法

- (void)createThread3{

[self performSelectorInBackground:@selector(run:) withObject:nil];

}

- (void)run:(NSString *)param{

for(NSInteger i = 0; i < 100000;i++){

NSLog(@"run------%@------%@",param,[NSThread currentThread].name);

}

}



//下面两种线程的优点:简单快捷 缺点是:无法对线程进行更详细的设置

//创建线程后自动启动线程

[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];

//隐式创建并启动线程

[self performSelectorInBackground:@selector(run) withObject:nil]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、多线程基础 基本概念 进程进程是指在系统中正在运行的一个应用程序每个进程之间是独立的,每个进程均运行在其专用且...
    AlanGe阅读 557评论 0 0
  • 原文地址 http://www.cnblogs.com/kenshincui/p/3983982.html 大家都...
    怎样m阅读 1,301评论 0 1
  • 1、简介:1.1 iOS有三种多线程编程的技术,分别是:1.、NSThread2、Cocoa NSOperatio...
    LuckTime阅读 1,367评论 0 1
  • 在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项。当然也会给出几种多线程的案...
    伯恩的遗产阅读 274,855评论 251 2,331
  • 我来给大家猜一个谜语,绿衣服,黑围巾、红肚子里黑宝宝。哈哈你们猜对了那就是“西瓜”。 我觉得西瓜像大河马,因为,西...
    故乡先生阅读 270评论 2 3