iOS多线程的创建

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    //第一种创建线程的方式

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread:) object:@"第一种创建线程的方式"];

    [thread start];//启动线程

    

    //第二种创建线程的方式

    //创建并执行线程

    [NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:@"第二种创建线程的方式"];

    

    //第三种创建线程的方式(NSObject带的方法)

    [self performSelectorInBackground:@selector(mutableThread:) withObject:@"第三种创建线程的方式"];

    

    //并行编程的方式创建线程

    //并行编程技术分类为:Operation Queue 和 GCD(Grand Central Dispatch),GCD包括:Dispatch Queue 和Dispatch Source

    //第四种创建线程的方式

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

    //会开启一个多线程,调用block

    [operationQueue addOperationWithBlock:^{

        NSLog(@"第四种创建线程的方式");

    }];

    

    //第五种创建线程的方式

    //线程池,线程队列,操作队列,用于管理线程,可以控制并发数,优先级

    //线程池的创建

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

    //设置线程池并发数

    //如果没有设置最大并发数,那么并发的个数是由系统内存和CPU决定的,可能内存多就开多一点,内存少就开少一点。

    //最大并发数不要乱写(5以内),不要开太多,一般以2~3为宜,因为虽然任务是在子线程进行处理的,但是cpu处理这些过多的子线程可能会影响UI,让UI变卡

    operationQueue2.maxConcurrentOperationCount = 1;

    

    //线程的创建

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1:) object:@"第五种创建线程的方式,第一个线程"];

    //设置线程的优先级,优先级高的任务,调用的几率会更大。

    [operation1 setQueuePriority:NSOperationQueuePriorityVeryLow];

    

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1:) object:@"第五种创建线程的方式,第二个线程"];

    //设置线程的优先级,优先级高的任务,调用的几率会更大。

    [operation2 setQueuePriority:NSOperationQueuePriorityVeryLow];

    

    NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1:) object:@"第五种创建线程的方式,第三个线程"];

    //设置线程的优先级

    [operation3 setQueuePriority:NSOperationQueuePriorityVeryHigh];

    

    //设置依赖

//    [operation1 addDependency:operation2];

    

    //将创建的线程添加到线程池中

//    [operationQueue2 addOperation:operation1];

//    [operationQueue2 addOperation:operation2];

//    [operationQueue2 addOperation:operation3];

    [operationQueue2 addOperations:@[operation1,operation3] waitUntilFinished:NO];

    NSLog(@"------");

    

    //第六种创建线程的方式GCD方式

    //创建一个队列,串行队列

    dispatch_queue_t queue = dispatch_queue_create("test", NULL);

    //创建异步线程

    dispatch_async(queue, ^{

        //多线程

        NSLog(@"第六种创建线程的方式-多线程");

        //回到主线程执行

        dispatch_sync(dispatch_get_main_queue(), ^{

            //主线程

            NSLog(@"第六种创建线程的方式-主线程");

        });

    });

    return YES;

}


- (void)mutableThread:(NSString *)data {

    

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

    //waitUntilDone:YES:表示等updateUI执行完才能往下执行

    [self performSelectorOnMainThread:@selector(updateUI:) withObject:@"update UI" waitUntilDone:YES];

}


- (void)updateUI:(NSString *)data {

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

    BOOL isMainThread = [NSThread isMainThread];

    if (isMainThread) {

        NSLog(@"是主线程");

    }

}


- (void)thread1:(NSString *)data {

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

//    NSLog(@"thread name=%@",[NSThread currentThread].name);

    BOOL isMultiThread = [NSThread isMultiThreaded];

    if (isMultiThread) {

        NSLog(@"是多线程");

    }

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

//        NSLog(@"thread1:%d",i);

//    }

}


- (void)thread2:(NSString *)data {

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

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

        NSLog(@"thread2:%d",i);

    }

}


转载于:https://my.oschina.net/hejunbinlan/blog/423196

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值