iOS 多线程的四种技术方案,美团架构师深入讲解Java开发

#pragma mark - 执行run方法

  • (void)run:(NSString *)param

{

// 当前线程是否是主线程

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

NSLog(@“—%@—%zd—%d”, [NSThread currentThread], i, [NSThread isMainThread]);

}

}复制代码

方法2和方法3的优点:快捷

方法1的优点:可以轻松拿到线程

线程间通信

线程间通信的体现

1个线程传递数据给另1个线程

在1个线程中执行完特定任务后,转到另1个线程继续执行任务

线程间通信的常用方法:小程序图片下载

-(UIImageView *)imageView

{

if (!_imageView) {

_imageView = [UIImageView new];

_imageView.frame = CGRectMake(0, 0, 300, 300);

_imageView.center = self.view.center;

[self.view addSubview:_imageView];

}

return _imageView;

}

  • (void)viewDidLoad {

[super viewDidLoad];

//监听线程结束的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle_threadexit:) name:NSThreadWillExitNotification object:nil];

// Do any additional setup after loading the view.

}

  • (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//第一种方式:先创建再启动线程

// 创建线程

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@“booob”];

// 线程启动了,事情做完了才会死, 一个NSThread对象就代表一条线程

[thread start];

//第二种:直接创建并启动线程

// 直接创建并启动线程

[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@“wang”];

//第三种:

// 直接创建并启动线程

[self performSelectorInBackground:@selector(run:) withObject:@“wang000”];

// 使线程进入阻塞状态

[NSThread sleepForTimeInterval:2.0];

//例子

// 获取图片的url

NSURL *url = [NSURL URLWithString:@“https://pages.github.com/images/slideshow/yeoman.png”];

// 另开1条线程 object用于数据的传递

NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(downLoadWithURL:) object:url];

thread3.name = @“downloadimage…”;

// 由于下面下载图片的耗时太长,应开启线程来完成

[thread3 start];

}

#pragma mark - 执行run方法

  • (void)run:(NSString *)param

{

// 当前线程是否是主线程

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

NSLog(@“—%@—%zd—%d”, [NSThread currentThread], i, [NSThread isMainThread]);

}

}

//线程直接的交互

// 下载图片

  • (void)downLoadWithURL:(NSURL *)url

{

NSLog(@“%s ,%s %@”,FILE,FUNCTION, [NSThread currentThread]);

// 下载图片

NSData *data = [NSData dataWithContentsOfURL:url];

// 生成图片

UIImage *image = [UIImage imageWithData:data];

// 返回主线程显示图片

[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

}

//处理线程结束事件

-(void)handle_threadexit:(NSNotification *)notify

{

NSThread * thread = (NSThread *)notify.object;

NSLog(@“+++++++++++++++ 线程 %@ 结束 ++++++++++++”,thread.name);

}复制代码

TIPS: 拓展,线程结束的通知

以上下载图片

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值