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. 有用的css片段

    1.背景渐变动画 CSS中最具诱惑的一个功能是能添加动画效果,除了渐变,你可以给背景色.透明度.元素大小添加动画.目前,你不能为渐变添加动画,但下面的代码可能有帮助.它通过改变背景位置,让它看起来有动 ...

  2. 将windows上的文件同步到linux上

    1.首先下载PSCP.exe,下载地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html 2.将PSCP.exe拷贝到C: ...

  3. java selenium后报错Element not found in the cache元素定位要重新赋值之前的定义

    习惯上把定位的元素在操作之前就定位好, 例如: WebElement element1=driver.findElement(...);      ----------declaration1 Web ...

  4. Java 中的 request 和response 区别

    1.response 属于重定向请求: 其地址栏的URL会改变: 会向服务器发送两次请求: 2. request 属于请求转发: 其地址栏的URL不会改变: 向服务器发送一次请求: 举一个区分它们的简 ...

  5. mybatis if test 不为空字符串或null

    <if test="type !=null and type !=''"> AND l.type=#{type,jdbcType=INTEGER} </if> ...

  6. 增强:MB1A物料价格检查

    INCLUDE:MM07MFP0_PICKUP_AUSFUEHREN FORM:pickup_ausfuehren这是MB1A的PAI的逻辑流里的字段检查 在FORM开始的地方: '. DATA:S_ ...

  7. 制作简单的2D物理引擎(一)——动力学基础

    一切的基础 点 在二维平面中,点$P$就是坐标$(x,y)$,点集就是一系列坐标的集合$\{P_1,P_2,...,P_n\}$,不过这个集合是有序的(顺时针). 向量 加减运算 $$\vec{P}\ ...

  8. Windows Store App 网络通信 HttpClient

    HttpClient类包含在System.Net.Http命名空间中,是向以URI标识的网络资源发送HTTP请求和接收HTTP响应的基类.在HTTP请求中使用该类可以向Web服务发送GET.POST等 ...

  9. 通过JS检测360浏览器

    如何通过JS检测360浏览器? 尝试了一大堆方法,网上大多数办法都是通过navigator.userAgent来判断,这可能在几年前是行得通的,现在360userAgent输出来跟谷歌除了版本号其余一 ...

  10. C# 扩展类

    C# 中提供一个非常实用的供能,扩展方法(Extension method) 扩展方法是通过额外的静态方法扩展现有的类型.通过扩展方法,可以对已有类型做自己想做的相关扩展.方法:定义静态类,扩展方法也 ...