zoukankan      html  css  js  c++  java
  • IOS线程学习(一)

    1.NSThread 

    官方的描述

    An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application.

    NSThread能控制一个线程的执行, 当你想在自己的线程执行OC方法时请用此类。对于执行较长的任务时这是也很有用的,不会堵住程序里面剩下需要执行的任务。

     NSThread *thread = [[NSThread alloc]initWithTarget:self selector:NSSelectorFromString(@"myThread:") object:nil];
     //启动线程 
     [thread start];
    //停止线程
    //if (![thread isCancelled]) {
    //[thread cancel];

    //    }

     1 -(void)myThread:(id)sender{
     2     NSLog(@"%@" , sender);
     3     @synchronized(self){
     4         while (true) {
     5             [NSThread sleepForTimeInterval:1];
     6             static int i = 0;
     7             NSLog(@"%i" , i++);
     8             if (i == 10) {
     9                 [NSThread exit];
    10             }
    11         }
    12     }
    13 }
    View Code

    结果:只打印到3时线程就终止了

    也可以用这个方法启动一个线程,但是不能是默认的Thread配置

    [NSThread detachNewThreadSelector:NSSelectorFromString(@"myThread:") toTarget:self withObject:@"myThread"];

    也等同于,这个方法在NSObject中被定义,只要是继承NSObject都可以这样用

    [self performSelectorInBackground:NSSelectorFromString(@"myThread:") withObject:@"myThread"];

     2.NSOperation

    目前我的理解就是一个封装操作某操作的,然后调用其start方法,就在主线程执行!!!

    如果不在主线程执行可以创建一个NSOperationQueue,然后将操作加入到其中执行

    其有两个子类NSBlockOperation和NSInvocationOperation

    NSBlockOperation

    The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.

     可见,NSBlockOperation是管理多个Block块的,而且只有所有的Block都执行完了才会变成finished状态;

        NSLog(@"%@ mainT = " ,[NSThread currentThread]);
        
        NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
            for (int i = 0; i<5; i++) {
                [NSThread sleepForTimeInterval:1];
                NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);
            }
        }];
        
        [blockOp addExecutionBlock:^{
            for (int i = 0; i<10; i++) {
                [NSThread sleepForTimeInterval:1];
                NSLog(@"block2>>>%i thread = %@" , i , [NSThread currentThread]);
            }
        }];
        
        [blockOp start];
        
        NSLog(@"到这了");

    运行结果:一个Block就在主线程,多个就会并行执行其他block

    NSInvocationOperation

    The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.

     可见,只能通过Action-Target模式加入一个操作

          NSLog(@"%@ mainThread = " ,[NSThread currentThread]);
        NSInvocationOperation *invocationOperation1= [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myOperation:) object:@"invocationOperation1"];
        
        [invocationOperation1 start];
        
         NSLog(@"到这了");
    
    -(void)myOperation:(id)sender{
            static int i = 0;
            while (i<4) {
                [NSThread sleepForTimeInterval:1];
                [NSThread isMainThread];
                NSLog(@"我是线程%@ %i", [NSThread currentThread] , i++);
            }
    }

    运行结果:

    2015-12-19 22:57:23.038 MYThread[4942:44405] <NSThread: 0x7ffcba50c190>{number = 1, name = main} mainThread = 
    2015-12-19 22:57:24.043 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 0
    2015-12-19 22:57:25.046 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 1
    2015-12-19 22:57:26.051 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 2
    2015-12-19 22:57:27.053 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 3
    2015-12-19 22:57:27.054 MYThread[4942:44405] 到这了

     3.NSOperationQueue

        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        
        NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(method1) object:nil];
        
        [queue addOperation:op];
        
        NSLog(@"到这了!");
    

     结果:

    2016-03-17 09:17:22.710 ViewAnim[580:12599] 到这了!
    2016-03-17 09:17:23.779 ViewAnim[580:12871] 111111
    2016-03-17 09:17:24.853 ViewAnim[580:12871] 111111
    2016-03-17 09:17:25.926 ViewAnim[580:12871] 111111
    2016-03-17 09:17:27.001 ViewAnim[580:12871] 111111
    2016-03-17 09:17:28.070 ViewAnim[580:12871] 111111
    2016-03-17 09:17:29.143 ViewAnim[580:12871] 111111
    。。。。。
    
  • 相关阅读:
    【结对编程收获】
    【第二次个人作业】结对作业Core第一组:四则运算生成PB16061082+PB16120517
    【第七周读书笔记】读《计算机系统详解》
    【第五周课堂作业】创新案例分析
    【第六周读书笔记】计算机体系结构读后感
    个人总结——马睿淳
    团队项目心得(6.15)
    团队项目心得
    《代码大全》读书笔记——第九周
    《我是一只IT小小鸟》(续)读书笔记——第八周
  • 原文地址:https://www.cnblogs.com/pigface/p/5056233.html
Copyright © 2011-2022 走看看