源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

实例解析iOS应用多线程开发中NSthread类的用法

  • 时间:2022-12-02 11:12 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:实例解析iOS应用多线程开发中NSthread类的用法
[b]一、NSthread的初始化 1.动态方法 [/b]
[u]复制代码[/u] 代码如下:
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;  // 初始化线程  NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  // 设置线程的优先级(0.0 - 1.0,1.0最高级)  thread.threadPriority = 1;  // 开启线程  [thread start];
  参数解析: selector :线程执行的方法,这个selector最多只能接收一个参数 target :selector消息发送的对象 argument : 传给selector的唯一参数,也可以是nil [b]2.静态方法 [/b]
[u]复制代码[/u] 代码如下:
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;  [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];  // 调用完毕后,会马上创建并开启新线程 
[b]3.隐式创建线程的方法 [/b]
[u]复制代码[/u] 代码如下:
[self performSelectorInBackground:@selector(run) withObject:nil]; 
[b]二、获取当前线程 [/b]
[u]复制代码[/u] 代码如下:
NSThread *current = [NSThread currentThread];
[b]三、获取主线程 [/b]
[u]复制代码[/u] 代码如下:
NSThread *main = [NSThread mainThread];
[b]四、暂停当前线程 [/b]
[u]复制代码[/u] 代码如下:
// 暂停2s  [NSThread sleepForTimeInterval:2];    // 或者  NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];  [NSThread sleepUntilDate:date]; 
[b]五、线程间的通信 1.在指定线程上执行操作 [/b]
[u]复制代码[/u] 代码如下:
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES]; 
[b]2.在主线程上执行操作 [/b]
[u]复制代码[/u] 代码如下:
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES]; 
[b]3.在当前线程执行操作 [/b]
[u]复制代码[/u] 代码如下:
[self performSelector:@selector(run) withObject:nil]; 
[b]六、优缺点 [/b]1.优点:NSThread比其他两种多线程方案较轻量级,更直观地控制线程对象 2.缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销 [b]七、下载图片的例子: [/b]新建singeView app 新建项目,并在xib文件上放置一个imageView控件。按住control键拖到viewControll er.h文件中创建imageView IBOutlet ViewController.m中实现:
[u]复制代码[/u] 代码如下:
//  //  ViewController.m  //  NSThreadDemo  //  //  Created by rongfzh on 12-9-23.  //  Copyright (c) 2012年 rongfzh. All rights reserved.  //    #import "ViewController.h"  #define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"  @interface ViewController ()    @end 
[u]复制代码[/u] 代码如下:
  @implementation ViewController    -(void)downloadImage:(NSString *) url{      NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];      UIImage *image = [[UIImage alloc]initWithData:data];      if(image == nil){                }else{          [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];      }  }    -(void)updateUI:(UIImage*) image{      self.imageView.image = image;  }      - (void)viewDidLoad  {      [super viewDidLoad];        //    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];      NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];      [thread start];  }    - (void)didReceiveMemoryWarning  {      [super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated.  }    @end 
线程间通讯 线程下载完图片后怎么通知主线程更新界面呢?
[u]复制代码[/u] 代码如下:
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
performSelectorOnMainThread是NSObject的方法,除了可以更新主线程的数据外,还可以更新其他线程的比如: 用:
[u]复制代码[/u] 代码如下:
performSelector:onThread:withObject:waitUntilDone:
运行下载图片: [img]http://files.jb51.net/file_images/article/201602/201621591510906.png?201611591523[/img] 图片下载下来了。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部