多线程
博客专区 > JK_Jack 的博客 > 博客详情
多线程
JK_Jack 发表于2年前
多线程
  • 发表于 2年前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

@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

 

共有 人打赏支持
粉丝 0
博文 6
码字总数 2658
×
JK_Jack
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: