ios多线程实现种类

NSThread

NSOperationQueue

NSObject
GCD

***************

1.NSThread

//线程

第一种

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

//

//    //给线程起名字

thread1.name=@"thread1";

//    //启动线程

[thread1 start];

//结束线程

[thread1 cancel];

第二种(不需要手动开启的)

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

2. NSOperation的两个子类

//一.

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

//   [inop start];

//二.

NSBlockOperation *blop=[NSBlockOperation blockOperationWithBlock:^{

NSLog(@"我是block");

}];

//创建队列

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

//设置最大并行数量

queue.maxConcurrentOperationCount=2;

//添加事件

[queue addOperation:inop];

[queue addOperation:blop];

3.NSObject

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

4.GCD

//GCD (先进先出 FIFO)

//串行:前一个任务完成,后一个任务才能执行

//并行:任务在派发时有序的,但是不应等第一个任务执行完成才开始.

//GCD队列分3中:主队列,全局队列,自定义队列

//1.使用主队列实现任务派发(串行),在主线程中

dispatch_queue_t mainQueue=dispatch_get_main_queue();

//1.1添加任务

dispatch_async(mainQueue, ^{

NSLog(@"第一个任务:当前线程是%@",[NSThread currentThread]);

});

//串行

//2.自定义队列

/*

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

//2.1添加任务

dispatch_async(myQueue, ^{

NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

});

//并行

//3.自定义队列

/*

dispatch_queue_t myQueue2=dispatch_queue_create("com.lanlan.myqueue", DISPATCH_QUEUE_CONCURRENT);

//3.1添加任务

dispatch_async(myQueue2, ^{

NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

});

//4.全局队列

/*

dispatch_queue_t globelQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//4.1添加任务

dispatch_async(globelQueue, ^{

NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

});

5.只保证执行一次

//保证只执行一次

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

NSLog(@"这里的代码只执行一次");

});

6.延迟5秒后执行

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

NSLog(@"等我5秒...");

});

7.//重复执行

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

NSLog(@"今天天气不错");

});

//子线程

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

//回到主线程

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

-(void)mymainTherad

{

//打印线程

NSLog(@"我在主线程里%@",[NSThread currentThread]);

}

多线程 NSThread GCD的更多相关文章

  1. iOS多线程 NSThread/GCD/NSOperationQueue

    无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...

  2. iOS中的多线程NSThread/GCD/NSOperation & NSOperationQueue

    iOS多线程有四套多线程方案: Pthreads NSThread GCD NSOperation & NSOperationQueue 接下来我来一个一个介绍他们 Pthreads 在类Un ...

  3. iOS 开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  4. iOS开发多线程篇—GCD介绍

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  5. iOS开发多线程篇—GCD的基本使用

    iOS开发多线程篇—GCD的基本使用 一.主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行. 提示:如果把任务放到主队列中进 ...

  6. iOS开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  7. [iOS]多线程和GCD

    新博客wossoneri.com 进程和线程 进程 是指在系统中正在运行的一个应用程序. 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 比如同时打开QQ.Xcode,系统就会分别 ...

  8. iOS开发多线程篇—GCD简介

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  9. 多线程:GCD

    多线程是程序开发中非常基础的一个概念,大家在开发过程中应该或多或少用过相关的东西.同时这恰恰又是一个比较棘手的概念,一切跟多线程挂钩的东西都会变得复杂.如果使用过程中对多线程不够熟悉,很可能会埋下一些 ...

随机推荐

  1. JS的全局变量&局部变量

    <script> var i=10; //全局变量 j = 20; //全局变量 function(){ var i=30; //局部变量 h = 40; //全局变量 } </sc ...

  2. sqlserver系统表操作

    查询表名中包含‘user’的方法Select * From sysobjects Where name like '%user%' 如果知道列名,想查找包含有该列的表名,可加上系统表syscolumn ...

  3. nginx中配置跨域支持功能

    vi /etc/nginx/nginx.conf 加入如下代码 http {  ###start####  add_header Access-Control-Allow-Origin *;  add ...

  4. hdu 1337 The Drunk Jailer

    http://acm.hdu.edu.cn/showproblem.php?pid=1337 #include <cstdio> #include <cstring> #def ...

  5. OVERLAY代码重入

    OVERLAY代码重入问题:自己遇到的问题 编写的测试代码如下: #include <stdio.h> #define BYTE unsigned char #define BYTE un ...

  6. 为什么选择使用 Dropbox 而不是其他品牌同步工具(不要加上多余的功能,要极致和专注)

    作者:吴锋链接:http://www.zhihu.com/question/19646859/answer/14707821来源:知乎著作权归作者所有,转载请联系作者获得授权. 窃以为楼主的问题,准确 ...

  7. python处理.seq文件

    # Deal with .seq format for video sequence # Author: Kaij # The .seq file is combined with images, # ...

  8. apache+php+mysql常见集成环境安装包

    http://www.thinksaas.cn/group/topic/33/ apache+php+mysql是常见php环境,在windows下也称为WAMP,对于初学者自选版本搭建总是会遇到一些 ...

  9. UESTC_How many good substrings CDOJ 1026

    Icerain likes strings very much. Especially the strings only consist of 0 and 1,she call them easy s ...

  10. 微型 Python Web 框架 Bottle - Heroin blog

    微型 Python Web 框架 Bottle - Heroin blog 微型 Python Web 框架 Bottle