多线程

@interface ViewController ()

@end

@implementation ViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view, typically from a nib.
}

- (
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}

- (
IBAction)button:(id)sender {
   
//子线程
//    NSThread *th = [[NSThread alloc] initWithTarget:self selector:@selector(th:) object:nil];
//    [th start];
    [
NSThread detachNewThreadSelector:@selector(th:) toTarget:self withObject:nil];
   
   
//线程互斥
   
//多线程队列
   
//创建队列
   
NSOperationQueue *q = [[NSOperationQueue alloc] init];
   
//在队列里创建添加子线程
    [q
addOperationWithBlock:^{
       
NSLog(@"q=%d",[[NSThread currentThread] isMainThread]);
       
//返回主线程的函数
        [
self performSelectorOnMainThread:@selector(mainThread:) withObject:nil waitUntilDone:YES];

    }];
   
// NSInvocationOperation创建子线程
   
NSInvocationOperation *inv =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(th:) object:nil];
    [q
addOperation:inv];
   
   
//GCD(c语言)创建线程
   
//1.创建队列
   
dispatch_queue_t queue = dispatch_queue_create("qq", NULL);
   
//2.创建子线程
   
dispatch_async(queue, ^{
       
//返回主线程
       
dispatch_sync(dispatch_get_main_queue(), ^{
           
        });
    });
   
   
   
   
   
}

-(
void)th:(id)object
{
   
NSLog(@"th=%d",[[NSThread currentThread] isMainThread]);
   
NSURL *url = [NSURL URLWithString:BaseUrl];
   
NSURLRequest *request = [NSURLRequest requestWithURL:url];
   
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   
UIImage *image = [UIImage imageWithData:data];
    [
self performSelectorOnMainThread:@selector(mainThread:) withObject:image waitUntilDone:YES];
}
-(
void)mainThread:(id)object
{
   
self.imageV.image = object;
}

@end

 

转载于:https://my.oschina.net/JKzhong/blog/518979

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值