iosGCD多线程之创建多线程
来源:互联网 发布:寻找满月英知 编辑:程序博客网 时间:2024/06/02 02:20
喔尽量分成一小节一小节的写。这样也难让大家看的清楚些。我这里有三种创建线程的方法。代码如下
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
//当用户点击屏幕,执行线程
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[selftestThread];
}
-(void)testThread
{
//取到当前线程,在这里就是主线程
NSThread *curThread = [NSThreadcurrentThread];
NSLog(@"curThread = %@",curThread);
// //取到主线程的方法
// NSThread *mainThread = [NSThread mainThread];
// NSLog(@"mainThread = %@",mainThread);
//
[selfcreateThread1];
}
-(void)createThread1
{
//一个NSThread对象就是一个线程
// 参数1,2 :指定线程中由参数1调用参数2的方法
// 参数3 :给参数2指定的方法传递实参
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(threadMain:)object:@"线程创建方式1"];
[thread setName:@"我叫二蛋"];
[thread start];
}
-(void)createThread2
{
[NSThreaddetachNewThreadSelector:@selector(threadMain:)toTarget:selfwithObject:@"线程创建方式2"];
}
-(void)createThread3
{
[selfperformSelectorInBackground:@selector(threadMain:)withObject:@"线程创建方式3"];
}
-(void)threadMain:(id)obj
{
for (int i =0; i<1000; i++) {
NSLog(@"i = %d,obj = %@,thread = %@",i,obj,[NSThread currentThread]);
}
}
当然大家还是需要多用,就像吃饭,吃的多了还能挑刺,继续下一篇,今天晚上3更。哈哈
- iosGCD多线程之创建多线程
- 多线程之线程创建
- C++ 多线程之创建多线程CreateThread
- 多线程之pthread_create创建线程
- Java多线程之创建线程
- 多线程系列之创建线程
- 多线程--多线程创建
- 【多线程】(一)创建多线程
- 创建多线程
- 创建多线程
- 创建多线程
- 多线程创建
- 创建多线程
- 创建多线程
- Java多线程开发系列之二:如何创建多线程
- iosGCD线程安全之资源共享
- 多线程专题之MFC创建线程(1)
- 多线程专题之MFC创建线程(2)
- LeetCode Largest Rectangle in Histogram (单调栈)
- Python中使用正则表达式
- man nc 中文版
- 使用redis作为缓存
- Hive学习路线
- iosGCD多线程之创建多线程
- Sort Colors —LeetCode
- 细说ASP.NET Cache 及其高级用法--服务端缓存
- poj K Best 最大化平均值 二分搜索
- Spiral Matrix
- iOS 网络编程4-发布异步请求
- eclipse中的常用快捷键
- 类集框架(二)
- WebView与JS交互