多线程

//    进程是指我们在系统中运行的每一个程序

//    线程就是我们在编程中编写的某一个功能代码块

    

/*

 线程的好处:

 可以把程序中占据时间长的任务放到线程中去处理,如图片、视频的下载,数据库的操作

 发挥多核处理器的优势,并发执行让系统运行的更快、更流畅,用户体验更好

 

 缺点:

 大量的线程降低代码的可读性

 更多的线程需要更多的内存空间

 当多个线程对同一个资源出现争夺的时候要注意线程安全的问题

 */

//    在ios中实现的方式:NSThread ,NSOperationQueue,GCD(Grand Central Dispatch)

    

//    mLock = [[NSLock alloc] init]; //@synchronized

////    NSThread

//    NSThread *tread1 = [[NSThread alloc] initWithTarget:self selector:@selector(dosome:) object:nil];

//    NSThread *tread2 = [[NSThread alloc] initWithTarget:self selector:@selector(dosome:) object:nil];

//    [tread1 setName:@"tread1"];

//    [tread2 setName:@"tread2"];

//

//    [tread1 start];

//    [tread2 start];

    

    

//    //    循环递归锁

//    mRecuLock=[[NSRecursiveLock alloc] init];

//    [NSThread detachNewThreadSelector:@selector(threadLock:)

// toTarget:self

//   withObject:nil];

    

    

//    条件锁

    mConditionLock=[[NSConditionLock alloc] initWithCondition:0];

//    NSOperationQueue

//    生产线程

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

//    消费线程

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

//    创建NSOperationQueue

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

    [mOperationQueue setMaxConcurrentOperationCount:2];

    [mOperationQueue addOperations:[NSArray arrayWithObjects:producers,consumers,nil] waitUntilFinished:YES];

    

    

/*

    GCD: GCD是纯C语言的,因此我们在编写GCD相关代码的时候,面对的函数,而不是方法

         GCD中的函数大多数都以dispatch开头

         GCD在libdispatch.dylib库中, 在程序运行的过程中会动态的加载这个库,不需要我们手动导入

    任务:执行的操作

    队列:存放任务

 

    将任务添加到队列中,GCD会自动将队列中的任务取出,放到对应的线程中执行

    任务的取出遵循队列的FIFO原则:先进先出,后进后出

 */

    

/*

   同步与异步:

   同步: 在当前线程中执行

   异步: 在另一条线程中执行

 */

    

//    获得全局的并发队列

    dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

 

//   并发队列: 可以让多个任务并发执行, 并发执行就是同时去执行  并发功能只有在异步(dispatch_async)函数下才有效

//    dispatch_async(queue, ^{

//        [NSThread sleepForTimeInterval:4];

//        NSLog(@"Thread 1");

//    });

//    dispatch_async(queue, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"Thread2");

//    });

//    dispatch_async(queue, ^{

//        [NSThread sleepForTimeInterval:3];

//        NSLog(@"Thread3");

//    });

 

//   串行队列: 让任务一个接着一个地执行, 一个任务结束再去执行下一个任务

//    创建串行队列  dispatch_get_main_queue()获得主队列

//    dispatch_queue_t  queuee= dispatch_queue_create("ddddd", NULL);

//    dispatch_async(queuee, ^{

//        [NSThread sleepForTimeInterval:4];

//        NSLog(@"Thread1");

//    });

//    dispatch_async(queuee, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"Thread2");

//    });

//    dispatch_async(queuee, ^{

//        [NSThread sleepForTimeInterval:3];

//        NSLog(@"Thread3");

//    });

   

//    监听一组任务

//    dispatch_group_t group = dispatch_group_create();

//    dispatch_group_async(group, queue, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"group1");

//    });

//    dispatch_group_async(group, queue, ^{

//        [NSThread sleepForTimeInterval:2];

//        NSLog(@"group2");

//    });

//    dispatch_group_async(group, queue, ^{

//        [NSThread sleepForTimeInterval:3];

//        NSLog(@"group3");

//    });

//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{

//        NSLog(@"updateUi");

//    });

    

//    前面的任务执行完之后 dispatch_barrier_async执行  然后后面的任务再去执行

//    dispatch_queue_t queue1 = dispatch_queue_create("gcdtest", DISPATCH_QUEUE_CONCURRENT);

//    dispatch_async(queue1, ^{

//        [NSThread sleepForTimeInterval:2];

//        NSLog(@"dispatch_async1");

//    });

//    dispatch_async(queue1, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"dispatch_async2");

//    });

//    dispatch_barrier_async(queue1, ^{

//        [NSThread sleepForTimeInterval:4];

//        NSLog(@"dispatch_barrier_async");

//    });

//    dispatch_async(queue1, ^{

//        [NSThread sleepForTimeInterval:1];

//        NSLog(@"dispatch_async3");

//    });

    

//    代码片段执行N

//    dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {

//        // 执行5

//        NSLog(@"11111111");

//    });

    

    

}

//NSThread线程执行的方法

-(void) dosome:(id)sender

{

    while (count >= 0)

    {

        [mLock lock];

        [NSThread sleepForTimeInterval:0.3];

        count --;

        NSLog(@"count = %d, thread = %@", count, [[NSThread currentThread] name]);

        [mLock unlock];

    }

//    通知主线程

//    [self performSelectorOnMainThread:@selector(doneThread:) withObject:nil waitUntilDone:YES];

}

-(void) doneThread:(id)sender

{

    NSLog(@"执行完成");

}

//主线程方法

-(void) producers:(id) sender

{

    while (TRUE)

    {

[mConditionLock lock];

 

[NSThread sleepForTimeInterval:0.3];

products++;

NSLog(@"生产者-当前产品数量:%d",products);

 

[mConditionLock unlockWithCondition:1];

NSLog(@"%ld",[mConditionLock condition]);

}

}

-(void) consumers:(id) sender

{

    while (TRUE)

    {

[mConditionLock lockWhenCondition:1];

[NSThread sleepForTimeInterval:0.3];

products--;

NSLog(@"消费者-当前产品数量:%d",products);

        

 

[mConditionLock unlockWithCondition:(products==0)?0 : 1];

NSLog(@"%ld",[mConditionLock condition]);

}

}

 

//

-(void) threadLock:(id)sender

{

NSLog(@"num:%d",[self num:3]);

}

//递归求和

-(NSInteger) num:(NSInteger)count

{

    [mRecuLock lock];

    if(count != 0)

    {

        -- count;

//        [self num:count];

        NSLog(@"dffff = %d", [self num:count]);

    }

    [mRecuLock unlock];

    return count;

// if (count==1) {

// return 1;

// }else {

// [mRecuLock lock];

// return count + [self num:count-1];

// [mRecuLock unlock];

// }

}

posted @ 2014-12-04 20:46  提灯走夜路  阅读(106)  评论(0编辑  收藏  举报