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];

//    }

 -(void)myThread:(id)sender{
NSLog(@"%@" , sender);
@synchronized(self){
while (true) {
[NSThread sleepForTimeInterval:];
static int i = ;
NSLog(@"%i" , i++);
if (i == ) {
[NSThread exit];
}
}
}
}

结果:只打印到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 = ; i<; i++) {
[NSThread sleepForTimeInterval:];
NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);
}
}]; [blockOp addExecutionBlock:^{
for (int i = ; i<; i++) {
[NSThread sleepForTimeInterval:];
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 = ;
while (i<) {
[NSThread sleepForTimeInterval:];
[NSThread isMainThread];
NSLog(@"我是线程%@ %i", [NSThread currentThread] , i++);
}
}

运行结果:

-- ::23.038 MYThread[:] <NSThread: 0x7ffcba50c190>{number = , name = main} mainThread =
-- ::24.043 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::25.046 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::26.051 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.053 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.054 MYThread[:] 到这了

 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
。。。。。

IOS线程学习(一)的更多相关文章

  1. 开源中国iOS客户端学习

    开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html ...

  2. IOS基础学习-2: UIButton

    IOS基础学习-2: UIButton   UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...

  3. IOS 线程处理 子线程

    IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585   IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...

  4. iOS阶段学习第一天笔记(Mac终端的操作)

    前言部分 原本从事的是.NET开发,一直在要不要转iOS 中犹豫徘徊,经过复杂的内心挣扎终于鼓起勇气辞职脱产学习iOS;希望通过四个月的 学习后能够拿到理想的薪资.以下是学习过程中的学习笔记,为了方便 ...

  5. ios网络学习------4 UIWebView的加载本地数据的三种方式

    ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...

  6. ios网络学习------6 json格式数据的请求处理

    ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...

  7. iOS之学习资源收集--很好的IOS技术学习网站

    点击图片也能打开相关的网站: https://boxueio.com/skill/swift http://ios.b2mp.cn/ http://gold.xitu.io/welcome/?utm_ ...

  8. ios开发之OC基础-ios开发学习路线图

    本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...

  9. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

随机推荐

  1. unity-点乘和叉乘的应用

    http://blog.csdn.net/oskytonight/article/details/38900087 点乘:两个向量点乘得到一个标量 ,数值等于两个向量长度相乘后再乘以二者夹角的余弦值 ...

  2. SVM原理与实践

    SVM迅速发展和完善,在解决小样本.非线性及高维模式识别问题中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中.从此迅速的发展起来,已经在许多领域(生物信息学,文本和手写识别等)都取 ...

  3. 错误: java.lang.reflect.InvocationTargetException

    错误: java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...

  4. 转载C#下RSA算法的实现(适用于支付宝和易宝支付)

    RSA算法代码: using System; using System.Collections.Generic; using System.Text; using System.IO; using S ...

  5. ajax无刷新删除、复制 THINKPHP

    一.jquery 代码: <SCRIPT LANGUAGE=javascript> function delGoods(goodsID) {      if(confirm( " ...

  6. spring beans

    所 有 使 用 XML 文 件 进 行 配 置 信 息 加 载 的 Spring IoC 容 器 , 包 括 BeanFactory 和ApplicationContext的所有XML相应实现,都使用 ...

  7. IOS常用框架

    IOS开发中有用的第三方库 #Objective-C中最受瞩目库 [链接](https://github.com/languages​​/Objective-C/most_watched) * [th ...

  8. 在EC2上搭建L2TP over IPSec VPN服务器

    注意(:wq保存文件 putty登陆用户名为ec2-user) 安装与配置: 环境介绍: OS:CentOS 6.4 x86_64 Minimal 1. 修改 /etc/sysctl.conf,新增如 ...

  9. liunx下,只获取主机的IP?

    命令: ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}'

  10. css个人随笔,适合新手总结整理

    CSS的3种引用方式:1.外部样式表 都是在head标签内使用Link标签来引用的.2.内部样式表 <style type="text/css"> </style ...