zoukankan      html  css  js  c++  java
  • 多线程

    - (void)viewDidLoad {

        [super viewDidLoad];

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

    }

    - (IBAction)click:(id)sender {

    //新线程.

        //NSOperation是一个抽象的类,使用:自己定义一个类,继承它,使用系统提供的类:

        

      //NSOperation *p=nil;

    //    

    //    //1

    //    MyOperation *o=[[MyOperation alloc]init];

    //    [o start];

    //    //2

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

    //    [o1 start];

       //3

        

        NSBlockOperation *b=[NSBlockOperation blockOperationWithBlock:

                             ^{

                          [self test];

                             }

                             ];

        

        NSBlockOperation *c=[NSBlockOperation blockOperationWithBlock:

                             ^{

                                 [self test];

                             }

                             ];

        

        NSBlockOperation *d=[NSBlockOperation blockOperationWithBlock:

                             ^{

                                 [self test];

                             }

                             ];

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

        queue.maxConcurrentOperationCount=2;//同时并发运行的线程为3;

        //同步执行,异步执行

        [c addDependency:b];

        [d addDependency:c];

        [queue addOperation:b];

        [queue addOperation:c];

        [queue addOperation:d];

        //执行没有顺序 。cpu开线程

     //   [b start];

        //多线程操作

    }

    - (void)test

    {

        int count=0;

        for (int i=0; i<635500000; i++) {

            count+=i;

        }

        NSLog(@"count1=%d",count);

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

    }

    - (IBAction)gcdClick:(id)sender {

        

        //并发对列

        dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);//预设的参数,假的

        

        dispatch_async(queue, ^{

            NSLog(@"线程1");

            NSLog(@"++%@",[NSThread currentThread]);//打印主线程

            

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

                

                NSLog(@"%d",i);

            }

            

        });

        

        dispatch_async(queue, ^{

            NSLog(@"线程2");

            

        });

        

        dispatch_async(queue, ^{

            NSLog(@"线程3");

            

        });

    }

    - (IBAction)gcd02:(id)sender

    {

        //创建串行并列

        dispatch_queue_t queue =dispatch_queue_create("net.scjy.queue", NULL);

        

        dispatch_async(queue, ^{

            NSLog(@"任务1");

            NSLog(@"++0%@",[NSThread currentThread]);//打印主线程

            

            //回到主界面

         //    dispatch_async(dispatch_get_main_queue(), ^{self->imgview=img});

        });

        

        dispatch_async(queue, ^{

            NSLog(@"任务2");

            NSLog(@"++00%@",[NSThread currentThread]);//打印主线程

        });

        

        dispatch_async(queue, ^{

            NSLog(@"任务3");

            NSLog(@"++000%@",[NSThread currentThread]);//打印主线程

        });

    }

  • 相关阅读:
    VB.NET中对象的克隆 利用了内存流内象和序列化
    关于对象组件编写的一点想法
    虽然有人说什么和平第一, 经济第一, 可是我怎么能不因为愤怒而发抖?
    用C# 调用MS speech引擎, 让电脑读文本, 或是存到WAV文件里去.
    抽空看了一下 dockpanel suite, 知道如何用了, 立此存照
    dn081A
    如何列出某类型的所有成员
    上周买了毛爷爷传
    【转载】MySQL双主双从高可用集群架构
    【转载】MySQL和Keepalived高可用双主复制
  • 原文地址:https://www.cnblogs.com/linximu/p/4402829.html
Copyright © 2011-2022 走看看