zoukankan      html  css  js  c++  java
  • IOS 多线程 NSOperation GCD

    1.NSInvocationOperation 

     NSInvocationOperation * op;

        NSOperationQueue * que = [[NSOperationQueuealloc]init];

        op = [[ NSInvocationOperationalloc]initWithTarget:selfselector:@selector(run:) object:@"aaa"];

        [que addOperation:op];

        //  这里不要使用   op start,否则就会出现住线程阻塞的现象。  默认情况下,调用了start方法后并不会开一条新线程去执行操作,而是在当前线程同步执行操作。只有将operation放到一个NSOperationQueue中,才会异步执行操作。

    -(void)run:(id)data

    {

        while (![op isCancelled])

        {

            [NSThreadsleepForTimeInterval:1];

            NSLog(@"run");

        }

    }

    通过   [op cancel];   来停止线程

     

    2.

     __block NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^(){

            while ( ![op isCancelled])

            {

                [NSThread sleepForTimeInterval:0.3];

                

                NSLog(@"cccc");

            }

        }];

     

    NSOperationQueue * que = [[NSOperationQueuealloc]init];

    [que addOperation:op];

    这里也需要通过  queue来控制,使用start的话也会造成阻塞。

    这里的  op  需要使用 __block 来控制,因为在^(){}里面用到,否则不对。

     

    3.自定义 NSOperation

     

    #import <Foundation/Foundation.h>

    @interface MyOperation : NSOperation

    @end

     

    #import "MyOperation.h"

     

    @implementation MyOperation

     

    -(void)main

    {

        @autoreleasepool

        {

            while ( YES )

            {

                

                [NSThreadsleepForTimeInterval:0.3];

                

                if( [self isCancelled] )

                    break;

                

                NSLog(@"aaaaaa");

            }

        }

    }

     

    //  如何调用

    -(void)aaaa

    {

        static BOOL bFlog = NO;

        if( !bFlog )

        {

            _op = [[MyOperation alloc]init];

            NSOperationQueue * que = [[NSOperationQueuealloc]init];

            

            [que addOperation:_op];

        }

        else

        {

            [_op cancel];

        }

        bFlog = YES;

    }

     

    3.  GCD

     dispatch_async(dispatch_get_global_queue(0, 0), ^(void){

            

            

            for( NSInteger index = 0; index < 10; ++ index )

            {

                [NSThreadsleepForTimeInterval:0.3];

                

                NSLog(@"index:%d",index);

                

                dispatch_async(dispatch_get_main_queue(), ^(void){

                    

                    _label.text = [NSString stringWithFormat:@"index:%d",index];

                });

            }

     

        });

  • 相关阅读:
    TOPCoder(一)Time
    highchart柱状图 series中data的数据构造
    (转)myeclipse工程 junit不能运行 ClassNotFoundException
    reserve和resize区别
    ++ fatal error C1083: 无法打开预编译头文件:“.Debug outer.pch”
    初学lua --lua嵌入c++的一个问题(初始化lua出错,版本问题)
    .NET中字符串split的C++实现
    成员函数指针与高效C++委托 (delegate)
    Android.mk 用法介绍
    cocos2d-x学习之旅(五):1.5 使用eclipse编译cocos2d-x示例项目,创建cocos2d-x android项目并部署到真机
  • 原文地址:https://www.cnblogs.com/rollrock/p/3546490.html
Copyright © 2011-2022 走看看