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

//    }

 1 -(void)myThread:(id)sender{
 2     NSLog(@"%@" , sender);
 3     @synchronized(self){
 4         while (true) {
 5             [NSThread sleepForTimeInterval:1];
 6             static int i = 0;
 7             NSLog(@"%i" , i++);
 8             if (i == 10) {
 9                 [NSThread exit];
10             }
11         }
12     }
13 }

View Code

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

运行结果:

2015-12-19 22:57:23.038 MYThread[4942:44405] <NSThread: 0x7ffcba50c190>{number = 1, name = main} mainThread =
2015-12-19 22:57:24.043 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 0
2015-12-19 22:57:25.046 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 1
2015-12-19 22:57:26.051 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 2
2015-12-19 22:57:27.053 MYThread[4942:44405] 我是线程<NSThread: 0x7ffcba50c190>{number = 1, name = main} 3
2015-12-19 22:57:27.054 MYThread[4942:44405] 到这了

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

转载于:https://www.cnblogs.com/pigface/p/5056233.html

IOS线程学习(一)相关推荐

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

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

  2. [源码、文档、分享] iOS/iPhone学习系列、代码教程(转)

    iOS/iPhone学习系列.代码教程----~~~持续更新中~~~ http://www.devdiv.com/iOS_iPhone-iOS_iPhone%E5%AD%A6%E4%B9%A0%E7% ...

  3. iOS完整学习路线图

    --http://blog.csdn.net/q199109106q/article/details/8596506 --http://edu.51cto.com/roadmap/view/id-7. ...

  4. ios开发学习-手势交互(Gesture)效果源码分享

    qianqianlianmeng ios开发学习-手势交互(Gesture)效果源码分享 All Around Pull View 介绍:实现视图四个方向(上下左右)都能够拖动更新(pull to r ...

  5. 通用线程: 学习 Linux LVM

    为什么80%的码农都做不了架构师?>>>    通用线程: 学习 Linux LVM "逻辑卷管理"为存储器管理带来的魔力 Daniel Robbins ( dr ...

  6. C#线程学习的一点体会和总结

    C#线程学习如同在Java中一样,在c#中写一个多线程应用是非常简单的,本章将介绍如何在c#种开发多线程程序.在.net中线程是由System.Threading 名字空间所定义的.所以你必须包含这个 ...

  7. iOS 线程操作库 PromiseKit

    iOS 线程操作库 PromiseKit 官网:http://promisekit.org/ github: https://github.com/mxcl/PromiseKit/tree/maste ...

  8. ios开发学习笔记--Core Motion

    iOS开发学习笔记之CoreMotion-运动传感器 官网文档:CoreMotion Framework Reference 一.     简介 现在的苹果手机都基本有运动传感器,能够过获取到设备的加 ...

  9. Windows进程与线程学习笔记(九)—— 线程优先级/进程挂靠/跨进程读写

    Windows进程与线程学习笔记(九)-- 线程优先级/进程挂靠/跨进程读写 要点回顾 线程优先级 调度链表 分析 KiFindReadyThread 分析 KiSwapThread 总结 进程挂靠 ...

最新文章

  1. 004-安装CentOS7后需要的操作
  2. 第二十六篇 面向对象初识
  3. 我遇到的CocoaPods的问题(也许后期会解决,持续更新)
  4. Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)
  5. 【实验2】——模糊函数
  6. python编程快速上手第四章_《Python编程快速上手——让繁琐的工作自动化》读书笔记 第四章 列表...
  7. 【论文写作】SpringMVC学籍管理系统如何画用例图(2)
  8. XAMPP中启动tomcat报错的解决方法
  9. 鸿蒙os2.0官网公测报名,鸿蒙OS2.0公测版测试资格报名-鸿蒙OS2.0公测版测试资格报名官网地址预约 -友情手机站...
  10. 基于springboot的考研学习平台
  11. BigDecimal——大十进制-货币型-双精度-精确运算
  12. PostgreSQL查询优化器详解之逻辑优化篇
  13. 浏览器HTML5 写入文件
  14. 谷歌Chrome紧急更新补丁0day漏洞
  15. c datetime 格式化
  16. MS sqlserver数据库恢复出错 Exclusive access could not be obtained because the database is in use
  17. PBOC规范研究之四、文件结构及访问
  18. 2015中国智能硬件蛋年创新大会手记
  19. 如何在MacBook Pro上使用原彩显示功能?
  20. 在线配资平台哪家正规?排名在前的有哪些平台?

热门文章

  1. 【嵌入式】C语言中volatile关键字
  2. python安装方法3.8.2_Linux安装Python3.8.1的教程详解
  3. python iocp_记对协程增加IOCP支持时候踩过的一些坑
  4. 逆向入门--简单win32CK逆向手记
  5. 经典的 Fork 炸弹解析
  6. Codeforces 1480A. Yet Another String Game (阅读理解题)
  7. LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
  8. Mysql(2)——mysql的配置文件信息(基本信息)
  9. 汇编---输出AX的地址值
  10. 可以在没有main()的情况下编写C程序吗?