zoukankan      html  css  js  c++  java
  • 移动开发在路上-- IOS移动开发系列 多线程一

    类似于什么是进程什么是线程在这里我就不多浪费时间了(Google一下什么都有)!

    废话不多说先上图,我相信大家都是喜欢看图的人,俗话说得好,求图求真相吗?虽然这里只有屌丝一个但是真相还是会有的。。。

    码农的EQ有限,所以既没有太多煽情的部分了

    在Obj-c中线程的创建与启动

    首先说一下OC中有几种多线程的方式

        //创建多线程对象一

        NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(ChildThread:) object:@"子线程"];

        //开始运行多线程

        [thread start];

        //创建多线程对象二

        [NSThreaddetachNewThreadSelector:@selector(ChildThread:) toTarget:selfwithObject:@"子线程"];

        //创建多线程对象三

        [selfperformSelectorInBackground:@selector(ChildThread:) withObject:@"子线程"];

        //创建多线程对象四

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

        [threadQueue addOperationWithBlock:^(void){

            NSThread *t = [NSThread currentThread];

            if (![t isMainThread]) {

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

                    NSLog(@"---子线程---%d",i);

                }

       

            }

        }];

        

        //创建多线程对象五

        //创建一个线程队列

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

        //同时执行的并发数

        operationQueue.maxConcurrentOperationCount = 1;

        //创建一个线程对象

        NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(ChildThread:) object:@"子线程"];

        //创建一个线程对象

        NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(ChildThread2:) object:@"子线程2"];

        //设置优先级

        operation2.queuePriority = NSOperationQueuePriorityHigh;

        

        

        [operationQueue addOperation:operation1];

        [operationQueue addOperation:operation2];

        

        //创建多线程对象六

        dispatch_queue_t queueq=dispatch_queue_create("test", NULL);

        dispatch_async(queueq, ^{

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

                NSLog(@"---子线程1---%d",i);

            }

            

            dispatch_sync(dispatch_get_main_queue(), ^{

                BOOL isMain = [NSThread isMainThread];

                if (isMain) {

                    NSLog(@"主线程");

                }

            });

            

        });

    先创建一个项目

    我这里XCode版本是5.0.2

    创建一个新项目

    选择一个空的application    创建

    下一步

    点击 创建

    到这里我们这个项目就算创建好了。

    开始 coding 

    选择这个.m文件

    又补充了一下子

    到这里先告一段落

     持续更新中...

    那里有不对的请多提意见,互相学习!

    感觉有帮助的话,请帮忙推荐一下,大家的肯定才是对我最大的支持!

    作者:zhangwenjian
    出处:http://www.cnblogs.com/zhangwenjian
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

  • 相关阅读:
    Spring AOP Capabilities and Goal
    CDI Features
    Java Design Patterns
    Connector for python
    Spring reference
    a+1、&a+1、*(a+1)、*(&a+1)、*(*(&a+1))的区别
    int **p和int *p
    Hibernate注解
    功能测试
    零售商商品管理系统代码节选
  • 原文地址:https://www.cnblogs.com/zhangwenjian/p/3603282.html
Copyright © 2011-2022 走看看