Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

线程间的通讯

$
0
0

前言

什么叫做线程间通信:
在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信

 1、线程间通信的体现
 1)1个线程传递数据给另1个线程
 2)在1个线程中执行完特定任务后,转到另1个线程继续执行任务

2、线程间通信常用方法

 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
 - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

例子

/**

例子:

 */
- (void)downLoad{
//    NSString *str = @"http://d.hiphotos.baidu.com/image/h%3D200/sign=35a6dc72d7160924c325a51be407359b/86d6277f9e2f0708ab639124ee24b899a901f2b4.jpg";
    NSString *str = @"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png";
    NSURL *url = [NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    //回到主线程刷新UI界面Invokes a method of the receiver on the main thread using the default mode.
    //方法一:
    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:NO];// Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
//    [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
    //方法二:回到主线程执行 self.imageView的setImage:方法
//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    NSLog(@"%s",__func__);
}
#if 1
- (void)showImage:(UIImage *)image{
    NSLog(@"  begin  --%s",__func__);
    [NSThread sleepForTimeInterval:2];
    [self.imageView setImage:image];
    NSLog(@" end ---%s",__func__);
}
#endif
作者:u011018979 发表于2017/7/4 15:22:38 原文链接
阅读:49 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>