NSThread的基本用法
2015-06-14 10:24
323 查看
- (IBAction)btnClick { // 1.获得当前的线程 NSThread *current = [NSThread currentThread]; NSLog(@"btnClick---%@", current); // NSThread *main = [NSThread mainThread]; // NSLog(@"btnClick---%@", main); // 2.执行一些耗时操作 : 创建一条子线程 [self threadCreate]; } - (void)run:(NSString *)param { NSThread *current = [NSThread currentThread]; for (int i = 0; i<10000; i++) { NSLog(@"%@----run---%@", current, param); } } /** * NSThread的创建方式 * 隐式创建线程, 并且直接(自动)启动 */ - (void)threadCreate3 { // 在后台线程中执行 === 在子线程中执行 [self performSelectorInBackground:@selector(run:) withObject:@"abc参数"]; } /** * NSThread的创建方式 * 创建完线程直接(自动)启动 */ - (void)threadCreate2 { [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是参数"]; } /** * NSThread的创建方式 * 1> 先创建初始化线程 * 2> start开启线程 */ - (void)threadCreate { NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"]; thread.name = @"线程A"; // 开启线程 [thread start]; NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"]; thread2.name = @"线程B"; // 开启线程 [thread2 start]; }
相关文章推荐
- Android--PendingIntent 实现发送通知notification
- 浏览器网址请求Tomcat服务器UML图全过程
- mongo与Java之Mapreduce
- 工作之约法三章
- 日志为什么被低估?
- Linux系统编程——线程同步与互斥:读写锁
- Js 常用调试的方法
- GState API
- JAVA 泛型 - Class<T>
- 冒泡排序 Java
- 苹果开发 笔记(37)oc与JS调用
- SQL SERVER技术内幕之10 事务并发
- 二叉树遍历,递归,非递归
- GrammarState API
- 树莓派GPIO介绍
- ActiveMq-PTP入门例子
- pthread使用举例
- Oracle的表连接方式
- hdu 4548 初始化+二分 *
- GrammarPoint API