Objective-C-NSObject类的线程方法
2016-03-03 00:00
477 查看
摘要: oc中,NSObject 对象的 线程
demo,在iOS中NSObject 对象的 线程
output:
2.线程间通信
子线程加载数据,福线程更新UI
demo,在iOS中NSObject 对象的 线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //子线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(createThread:) object:@"first"]; [thread start]; //子线程 [NSThread detachNewThreadSelector:@selector(createThread:) toTarget:self withObject:@"second"]; //子线程,后台 [self performSelectorInBackground:@selector(createThread:) withObject:@"thrid"]; //主线程 [self performSelector:@selector(createThread:) withObject:@"forth"]; //主线程,因为onThread:[NSThread currentThread] [self performSelector:@selector(createThread:) onThread:[NSThread currentThread] withObject:@"fifth" waitUntilDone:NO];//改变yes,no NSLog(@"---fifth:Done---"); } - (void) createThread:(NSString *) who { NSLog(@"%@:%@",who, [NSThread currentThread]); }
output:
2.线程间通信
子线程加载数据,福线程更新UI
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [ self performSelectorInBackground:@selector(downloadImage:) withObject:@"http://www.sinaimg.cn/qc/photo_auto/photo/34/78/6713478/6713478_src.jpg" ]; } - (void) downloadImage:(NSString *) urlStr { NSLog(@"downLoading, thread is:%@", [NSThread currentThread]); NSURL *url = [NSURL URLWithString:urlStr]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; //perform.... 执行完 前面的操作后(指令)才会执行 updateUi [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES]; // [self updateUI:image]; } - (void) updateUI:(UIImage *)image { NSLog(@"updateUI:%@", [NSThread currentThread]); self.testImgView.image = image; }
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#控制台下测试多线程的方法
- 21天学习android开发教程之SurfaceView与多线程的混搭
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)
- C#实现多线程的Web代理服务器实例
- c#实现多线程局域网聊天系统