IOS多线程_NSThread和NSInvocationOperation

//虽然现在在多线程方面大多都在用GCD,当其他方式我们还是应该有所了解,给大家介绍一下NSThread和NSInvocationOperation的简单用法


@interfaceyxpViewController ()

{

   UIImageView *_imageView;

    

    //声明一个队列

   NSOperationQueue *_operationQueue;

}


@end


@implementation yxpViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    //初始化操作队列

    _operationQueue = [[NSOperationQueuealloc] init];

    

     //在这里限定了该队列只同时运行一个线程

    [_operationQueuesetMaxConcurrentOperationCount:1];

    

    //初始化一_ImageView

    _imageView=[[UIImageViewalloc] initWithFrame:CGRectMake(10,70, 300, 450)];

    _imageView.backgroundColor=[UIColorgrayColor];

    _imageView.animationDuration=3.0;

    _imageView.animationRepeatCount=0;

    [self.viewaddSubview:_imageView];

    

    //下面两种方法添加一个线程

    //第一种

    

    //创建一个NSInvocationOperation对象,并初始化到方法

    //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)

    //在这里,object后的值是想传递给前面方法的数据

    NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addTaskMethod:) object:_imageView];

    

    //当然也可以选择直接调用start方法开始,不过这个样做是同步的,也就是说还是要等后台线程执行完成才执行主线程

    //[theOp start];

    

    // Operation加入到队列中 是theOp和主线程同时执行,也就是异步线程,这样才能达到我们的效果

    [_operationQueue addOperation:theOp];

    

    //第二种

    //用[NSThread detachNewThreadSelector:@selector(addTaskMethod:) toTarget:self withObject:nil]; 也可以增加一个线程 等效于上一种

    //[NSThread detachNewThreadSelector:@selector(addTaskMethod:) toTarget:self withObject:nil];


}

// 运行另外一个线程的方法

- (void)addTaskMethod:(id)data

{

    NSString *url1=@"http://h.hiphotos.baidu.com/image/w%3D230/sign=b2d5c289123853438ccf8022a311b01f/91ef76c6a7efce1b1ae9f92fad51f3deb58f6510.jpg";

    NSString *url2=@"http://h.hiphotos.baidu.com/image/pic/item/d058ccbf6c81800aae834e8bb33533fa838b47d5.jpg";

    NSString *url3=@"http://d.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494eec3ba65132ff5e0fe99257e1b.jpg";

    NSString *url4=@"http://g.hiphotos.baidu.com/image/pic/item/a6efce1b9d16fdfa81f4ace4b68f8c5494ee7b1b.jpg";

    NSString *url5=@"http://g.hiphotos.baidu.com/image/pic/item/d6ca7bcb0a46f21f70031fdbf4246b600c33ae07.jpg";

    

    

   NSArray *array=[[NSArrayalloc] initWithObjects:url1,url2,url3,url4,url5,nil];

    

    NSMutableArray *imageArray=[[NSMutableArrayalloc] initWithCapacity:20];

   for (NSString *stringin array) {

       UIImage *image=[selfgetImageFromURL:string];

        [imageArrayaddObject:image];

    }

    

   _imageView.animationImages=imageArray;

    

    //回到主线程调用 在非线程中不能调用有关UI线程(主线程)的方法的,你可以直接调用一下看是否有效

    [selfperformSelectorOnMainThread:@selector(startImageViewAnimating)withObject:nilwaitUntilDone:YES];

}

- (void)startImageViewAnimating

{

    [_imageView startAnimating];

}

//下载图片的方法

-(UIImage *) getImageFromURL:(NSString *)fileURL {

    NSLog(@"执行图片下载函数");

   UIImage * result;

    NSData * data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:fileURL]];

    result = [UIImageimageWithData:data];

   return result;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在网络编程中,多线程编程是一种常用的技术,可以提高程序的并发性和性能。下面是一些关于多线程编程的常用方法和注意事项: 1. NSThreadNSThreadiOS中最底层的线程类,它可以通过类方法或实例方法来创建线程。使用NSThread可以设置线程的名称、优先级,以及控制线程的睡眠和退出等操作。 2. 线程调度:在多线程编程中,多个线程会并发运行,但线程的执行顺序是由CPU调度器决定的,程序员无法控制。多个线程会同时竞争CPU资源,谁先抢到资源谁就先执行,所以多线程的执行顺序是随机的。 3. 多线程的创建:在iOS开发中,常用的多线程编程方式有三种:NSThread、GCD和NSOperationNSThread是最底层的线程类,可以直接操作线程的各种属性和方法。GCD(Grand Central Dispatch)提供了一种高效的并发编程模型,可以通过队列来管理任务的执行。NSOperation是基于GCD的更高层次的封装,提供了更多的控制和管理线程的功能。 4. 线程的创建顺序:在多线程编程中,并不能保证哪个线程会先运行,即无法确定新创建的线程或调用线程哪个会先执行。新创建的线程可以访问进程的地址空间,并继承调用线程的浮点环境和信号屏蔽字,但挂起信号集会被清除。 总结来说,多线程编程是一种提高程序并发性和性能的技术,在网络编程中尤为重要。通过使用NSThread、GCD或NSOperation等方法,可以实现多线程的创建和管理。然而,程序员无法控制线程的执行顺序,因为线程的调度是由CPU调度器决定的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [IOS多线程基础(OC)](https://blog.csdn.net/yong_19930826/article/details/105857055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [UNIX环境高级编程笔记](https://blog.csdn.net/w_x_myself/article/details/128613534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值