基础多线程的介绍

第一种需手动开启

1.

NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(runFor) object:nil];

[thread start];

线程需自动开启

2.

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

第二种的线程自

1.

 NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(runFor) object:nil];

NSOperationQueue *queue = [[NSOperationQueue alloc]init];

 [queue addOperation:operation];

2.

- (void)createOperation{

    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{        

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

            NSLog(@"%d",i);

        }

        [self runFor];

    }];

    NSLog(@"NSBlockOperation == %s",__func__);

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    [queue addOperation:block];

NSOBject自带的线程

[self performSelectorInBackground:@selector(runFor) withObject:self];

GCD主要使用

- (void)action{

//    //GCD 全名grand central dispatch

//    //并发执行的队列 DISPATCH_QUEUE_CONCURRENT

//    //还有一个同步DISPATCH_QUEUE_SERIAL

//    dispatch_queue_t concurrent = dispatch_queue_create("1", DISPATCH_QUEUE_CONCURRENT);

//    //异步执行队列,block中的代码添加到创建的队列中

//    dispatch_async(concurrent, ^{

//        for (int i = 0; i < 10; i++) {

//            NSLog(@"i == %d",i);

//        }

//    });

//   dispatch_async(concurrent, ^{

//       for (int j = 0; j < 10; j++) {

//           NSLog(@"j == %d",j);

//       }

//   });

//    

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      

      NSURL *url = [NSURL URLWithString:@"http://img3.douban.com/view/event_poster/hlarge/public/365aa3c50b5ba01.jpg"];

      NSData *data = [NSData dataWithContentsOfURL:url];

      UIImage *image = [UIImage imageWithData:data];

      NSLog(@"%s",__func__);

      dispatch_async(dispatch_get_main_queue(), ^{

          [self.imageView setImage:image];

          NSLog(@"%s",__func__);

      });


//      [self performSelectorOnMainThread:@selector(myAction:) withObject:image waitUntilDone:YES];

//      

      

  });

    

    

}



 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值