美文网首页
多线程的基本概念和用法

多线程的基本概念和用法

作者: 名字太多不会起 | 来源:发表于2016-02-10 23:16 被阅读49次

1.进程的基本概念

  • 每一个进程都是一个应用程序,都有独立的内存空间,一般来说一个应用程序存在一个进程,但也存在多个进程的情况.
  • 同一个进程中的线程共享内存中的内存和资源.

2.多线程的基本概念

  • 每一个程序都有一个主线程,程序启动时创建(调用main来启动)
  • 主线程的生命周期是和应用程序绑定的,程序退出(结束)时,主线程也就停止了
  • 多线程技术表示,一个应用程序有多个线程,使用多线程能增强CPU的使用率,防止主线程堵塞.
  • 任何有可能堵塞主线程的任务不要再主线程执行(访问网络)

废话不多说上代码

//在AppDelegate.m建一个方法,
- (void)mutableThread:(NSString *)t{
    for (int i = 0; i < 200; i ++) {
        NSLog(@"-多线程-1-%d",i);
    }
//在didFinishLaunching里(其余创建多线程也在这里)
    for (int i = 0; i < 200; i ++) {
        NSLog(@"-主线程-%d",i);
    }

NSTheadd:第一种

//创建多线程对象
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread:) object:@"test"];
    //开始运行多线程
    [thread start];

NSTheadd:第二种

[NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:@"test"];

NSTheadd:第三种

 [self performSelectorInBackground:@selector(mutableThread:) withObject:nil];

NSTheadd:第四种

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue addOperationWithBlock:^{
       // 里面运行多线程的方法
        for (int i = 0; i < 200; i ++) {
            NSLog(@"-多线程-%d",i);
        }
    }];

NSOperationQueue

 //创建一个线程队列
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    //设置线程并发数的个数
    operationQueue.maxConcurrentOperationCount = 1;
    //创建一个线程操作对象
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread:) object:nil];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread2:) object:nil];
    //设置线程数优先级
    operation1.queuePriority = NSOperationQueuePriorityHigh;
    
    
    NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread3:) object:nil];
    [operationQueue addOperation:operation1];
    [operationQueue addOperation:operation2];
    [operationQueue addOperation:operation3];
    

- (void)mutableThread:(NSString *)t{
    for (int i = 0; i < 200; i ++) {
        NSLog(@"-多线程-1-%d",i);
    }
 // 跳到主线程
[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];
    
}
- (void)mutableThread2:(NSString *)t{
    for (int i = 0; i < 100; i ++) {
        NSLog(@"-多线程-2-%d",i);
    }
}
- (void)mutableThread3:(NSString *)t{
    for (int i = 0; i < 100; i ++) {
        NSLog(@"-多线程-3-%d",i);
    }
}

GCD

dispatch_queue_t queue = dispatch_queue_create("test", NULL);
    dispatch_async(queue, ^{
        for (int i = 0; i < 200; i ++) {
            NSLog(@"-多线程-%d",i);
        }
    });
    BOOL isMuliti = [NSThread isMultiThreaded];
    if (isMuliti) {
        NSLog(@"多线程");
    }
    dispatch_sync(dispatch_get_main_queue(), ^{
        BOOL isMain = [NSThread isMainThread];
        if (isMain) {
            NSLog(@"主线程");
        }
    });
    //次线程还是主线程
   dispatch_sync(dispatch_get_main_queue(), ^{
       BOOL isMain = [NSThread isMainThread];
       if (isMain) {
           NSLog(@"主线程");
       }

   });

- (void)mainThread {
    BOOL isMain = [NSThread isMainThread];
    if (isMain) {
        NSLog(@">>>>>>>>>>>>>>>>>>>>3主线程");
    } else {
        NSLog(@"其他线程");
    }
}

3.三种多线程技术的对比

•NSThread:–优点:NSThread 比其他两个轻量级,使用简单–缺点:需要自己管理线程的生命周期、线程同步、加锁、睡眠以及唤醒等。线程同步对数据的加锁会有一定的系统开销
•NSOperation:–不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上–NSOperation是面向对象的
•GCD:–Grand Central Dispatch是由苹果开发的一个多核编程的解决方案。iOS4.0+才能使用,是替代NSThread, NSOperation的高效和强大的技术–GCD是基于C语言的

未经博主允许,不得转载!
#好了各位晚安!哈哈哈哈哈,

相关文章

网友评论

      本文标题:多线程的基本概念和用法

      本文链接:https://www.haomeiwen.com/subject/tjpjkttx.html