zoukankan      html  css  js  c++  java
  • 主线程和分线程

    //

    //  ViewController.m

    //  MultiThread

    //

    //  Created by mac on 15-9-28.

    //  Copyright (c) 2015年 zy. All rights reserved.

    //

     

    #import "ViewController.h"

    #import "MyOpearation.h"

    #import "My.h"

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        

    //    多线程原理

    //    1.一个CPU在同一个时间内,是只能执行一个线程的

    //    2.CPU在多个线程之间来回调度(跳转),如果跳转速度很快,就造成了多个线程的假象.

        

    //    实现多线程的方法

        /*

         1.NSThread

         2.NSOperationQueue

         3.GCD grand centrol dispatch  (调度中心)

         使用GCD 可以创建 串行 线程队列  可以创建 并行线程队列

    //     串行:队列中A,B,C 三个任务 , 依次执行

    //     并行:队列中A,B,C 三个任务 , 并列(全部同时)执行

         */

     /*********************************************/

    //使用 NSOperation 和NSOperationQueue结合实现多线程

        /*

         1.创建NSOperation操作对象

         2.把创建的NSOperation对象放到NSOperationQueue操作队列中

         3.系统自动会去NSOperationQueue操作队列中获取一个NSOperation操作对象,并开启一个分线程执行操作.

         */

    //    NSOperation 是一个抽象类    不具备创建对象的能力不能使用它来创建一个对象

        //    只能使用NSOperation的子类来创建一个NSOperation的对象(//                               1.NSInvocationOperation

        //   2. NSBlockOperation

        //   3.自定义  NSOperation的子类

    //   使用 NSInvocationOperation 创建操作对象

    //    1. 目标     2.方法名称   3. 方法参数

         /**********************************************************/

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

    //    开始执行操作,一旦开始执行操作,就会执行操作指定的方法OpearationText1

    //    [opearation1 start];

    //    NSInvocationOperation 创建的操作对象默认是在主线程中执行的

     /**********************************************************/

    //    使用 NSBlockOperation来创建一个操作对象

    //    bloc块中  是操作具体实现的内容

    //    其实只是把OpearationText方法中的内容写到Block块中;

    //    NSBlockOperation * blockOpearationTest2=[NSBlockOperation  blockOperationWithBlock:^{

    //        NSLog(@"这是blockOpearationTest2执行的内容------%d",[NSThread isMainThread]);

    //        [NSThread sleepForTimeInterval:2];

    //    }];

    //    可以在blockOpearationTest2 任务中添加其他的一些任务

    //    [blockOpearationTest2 addExecutionBlock:^{

    //        NSLog(@"这是blockOpearationTest2执行的内容2------%d",[NSThread isMainThread]);

    //        [NSThread sleepForTimeInterval:10];

    //    }];

    //    [blockOpearationTest2 start];

    //    NSBlockOperation 创建的操作对象默认是在主线程中执行的

    //    如果NSBlockOperation 中有附加操作内容,那么额外添加的操作内容是在分线程中执行的

     /**********************************************************/

    //    自定义操作对象

    //    自定义一个类  继承NSOperation

    //    MyOpearation * MyOpearatiotest3=[[MyOpearation alloc] init];

    //    自定义操作开始执行,执行main方法里边的内容

    //    [MyOpearatiotest3 start];

    //    自定义操作对象也是在主线程中执行的

       /**********************************************************/

        

    //    创建一个操作对象,然后把操作对象放入queue对列中

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

        

    //    对列中最大的线程并发数  也就是说对列可以一次性的执行多少个分线程

    //    [queue setMaxConcurrentOperationCount:5];

    //    如果队列中多余5个线程,那么第六个线程必须要等待前5个线程中某一个执行完毕才能执行

    //    for (int i=0; i<10; i++)

    //    {

    //        MyOpearation * opear4=[[MyOpearation alloc]init];

    //        [queue addOperation:opear4];

    //    }

    //    [queue addOperation:blockOpearationTest2];

    //    操作对象的顺序取决于

    //    1.对象之间的依赖关系

    //    2.对象优先级

    //    MyOpearation * opear5=[[MyOpearation alloc]init];

    //    My * opear6=[[My alloc]init];

    //    addDependency 从属   即5对象依赖于6

    //    当6执行完毕之后才能执行5   与放入队列中的先后顺序是没有关系的 当他们都放入队列中时现执行6后执行5

    //    [opear5 addDependency:opear6];

    //    设置操作的优先级

    //    Priority:优先级

    //    先执行优先级高的在执行优先级低的

    //    [opear5 setQueuePriority:NSOperationQueuePriorityVeryHigh];

    //    [queue addOperation:opear5];

    //    [queue addOperation:opear6];

    //    当把操作对象放入队列中之后,不需要调用对象的start方法,操作对象会自动执行

     

    }

    //-(void)OpearationText1

    //{

    //    NSLog(@"这是opearation1执行的方法");

    //    NSLog(@"OpearationText1%d",[NSThread isMainThread]);

    //}

    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

  • 相关阅读:
    [Functional Programming] Running though a serial number prediction functions for tagging, pairing the result into object
    [Angular] Communicate with Angular Elements using Inputs and Events
    [Tools] Target specific browsers with babel-preset-env and the babel pollyfill (browserslist)
    [Webpack] Externalize Dependencies to be Loaded via CDN with webpack
    [Webpack] Analyze a Production JavaScript Bundle with webpack-bundle-analyzer
    [Algorithm] Largest sum of non-adjacent numbers
    [Angular] Use ngx-build-plus to compile Angular Elements
    [Algorithm] Search for matching words
    Is life always hard?
    poj3177 Redundant Paths
  • 原文地址:https://www.cnblogs.com/wangzhen-Me/p/4844886.html
Copyright © 2011-2022 走看看