IOS 多線程/GCD

來源:互聯網
上載者:User

標籤:style   io   ar   os   使用   sp   for   on   div   

多線程  —單利

+ (SingleHandel *)shareModel

{

    static dispatch_once_t onceQueue;

    dispatch_once(&onceQueue, ^{

        shareSingle = [[SingleHandel alloc] init];

    });

    return shareSingle;

}

/* //第一種線程開啟方式

    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(threadAction) object:nil];

    //給你的線程起個名字

    thread1.name = @"thread1";

    //啟動線程

    [thread1 start];

    //關閉

    [thread1 cancel];*/

    //第二種線程開啟方式(程式啟動並執行時候自動開啟線程)

    /*[NSThread detachNewThreadSelector:@selector(threadAction) toTarget:self withObject:nil];*/

    //第三種繼承與NSObjet

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

    


//    NSOperation

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

    NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"我是block");

    }];

//    [blockOp start];

    //建立隊列

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

    //設定最大同時執行數量

    queue.maxConcurrentOperationCount = 1;

    

    //添加事件

    [queue addOperation:inOp];

//    sleep(1);

    [queue addOperation:blockOp];

    

}


- (void)invocation

{

//    NSLog(@"%@",[NSThread currentThread]);

    NSLog(@"我是方法");

}




/-----------------------------------------------------GCD----------------------------------------------------------------/

多線程---GCD

- (IBAction)buttonAction:(id)sender

{

    

    //GCD(先進先出first in first out 簡稱FIFO)

    //串列 前一個執行完成之後,後一個任務才能執行

    //並行 任務在派發時時有序的,不用等到第一個任務執行完成才開始下一個(也就是一塊執行)

    

    //GCD分為三種:主隊列,全域隊列,自訂隊列

    /*--------------------主隊列(串列)----------------------*/

    //使用主隊列實現任務派發(串列),在主線程中

    /*dispatch_queue_t mainQueue = dispatch_get_main_queue();

    //新增工作

    dispatch_async(mainQueue, ^{

        NSLog(@"第一個任務%@",[NSThread currentThread]);


    });

    dispatch_async(mainQueue, ^{

        NSLog(@"第二個任務:%@",[NSThread currentThread]);

    });

    dispatch_async(mainQueue, ^{

        NSLog(@"第三個任務:%@",[NSThread currentThread]);

    });*/

    /*--------------------自訂隊列(串列)----------------------*/

    //自訂隊列

   /* dispatch_queue_t myQueue = dispatch_queue_create("com.myqueue", DISPATCH_QUEUE_SERIAL);

    //新增工作

    dispatch_async(myQueue, ^{

        NSLog(@"第一個任務%@",[NSThread currentThread]);

    });

    dispatch_async(myQueue, ^{

        sleep(3);

        NSLog(@"第二個任務:%@",[NSThread currentThread]);

    });

    dispatch_async(myQueue, ^{

        NSLog(@"第三個任務:%@",[NSThread currentThread]);

    });*/

    /*--------------------自訂隊列(並行)----------------------*/

   /* dispatch_queue_t myqueue1 = dispatch_queue_create("com.s", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(myqueue1, ^{

        NSLog(@"第一個任務%@",[NSThread currentThread]);

        

    });

    dispatch_async(myqueue1, ^{

        //可以延時

        sleep(3);

        NSLog(@"第二個任務%@",[NSThread currentThread]);

        

        

    });

    dispatch_async(myqueue1, ^{

        NSLog(@"第三個任務%@",[NSThread currentThread]);

        

    });*/

    

    /*--------------------全域隊列(並行)----------------------*/

   /* dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(globalQueue, ^{

        sleep(2);

        NSLog(@"第一個任務:%@",[NSThread currentThread]);

        

        

    });

    dispatch_async(globalQueue, ^{

        NSLog(@"第二個任務:%@",[NSThread currentThread]);

        

    });

    dispatch_async(globalQueue, ^{

        NSLog(@"第三個任務:%@",[NSThread currentThread]);

        

    });*/

    /*---------------------------------延時---------------------------------------------*/

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self afther];

    });

    

    /*-----------------------------重複執行---------------------------------------------*/

   

    dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {

        [self afther];


    });

    /*-----------------------------執行一次---------------------------------------------*/

    static dispatch_once_t onceQueue ;

    dispatch_once(&onceQueue, ^{

        NSLog(@"我就執行一次");

    });

    

    /*----------------------------------線程通訊---------------------------------*/

    //遇到線程通訊,執行他的方法,可以回到主線程(如果正在執行子線程,走這個方法可以跳出子線程到主線程)

    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil ];

    

    

}

- (void)newThread

{

    NSLog(@"我是子線程");

    //回到主線程

    [self performSelectorOnMainThread:@selector(afther) withObject:self waitUntilDone:NO];

}

-(void)afther

{

    NSLog(@"sad");

}


IOS 多線程/GCD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.