一:NSThread的概念:

二:NSThread的使用:

1.创建一个Thread

1.1第一种方法:

- (void)test1{
NSString *str = @"zhengli"; NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:str];// 注意object:这个参数是提供个run方法的。
[thread start];// 注意用上述方法创建NSThread对象时要使用start这个方法 }

这种方法需要使用[thread start]才会开始执行线程里的内容。

1.2第二种方法:

- (void)test2{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"zhengli"];
}

比较简单易用。

1.3第三种方法

- (void)test3{// 隐式创建线程对象
[self performSelectorInBackground:@selector(run) withObject:nil]; }

三种方法总结:可以看到虽然第一种方法比较麻烦,需要调用[thread start]这个对象方法,但是可以得到一个线程对象的实例,在下面的设置属性的过程中可能会更加方便。而后面的两种方法虽然使用起来简单,无需再次调用对象方法,但是没法得到NSThread对象,设置起属性来会麻烦一些。

三:NSThread的属性:

 @property (readonly, getter=isExecuting) BOOL executing NS_AVAILABLE(10_5, 2_0);
@property (readonly, getter=isFinished) BOOL finished NS_AVAILABLE(10_5, 2_0);
@property (readonly, getter=isCancelled) BOOL cancelled NS_AVAILABLE(10_5, 2_0); @property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0); @property NSUInteger stackSize NS_AVAILABLE(10_5, 2_0); @property (readonly) BOOL isMainThread NS_AVAILABLE(10_5, 2_0); @property double threadPriority NS_AVAILABLE(10_6, 4_0); // To be deprecated; use qualityOfService below @property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0); // read-only after the thread is started

其中

@property double threadPriority NS_AVAILABLE(10_6, 4_0); // To be deprecated; use qualityOfService below

这个属性是线程的优先级,官方注释如下:

@property double threadPriority

Description

The thread priority to use when executing the operation

The value in this property is a floating-point number in the range 0.0 to 1.0, where 1.0 is the highest priority. The default thread priority is 0.5.

The value you specify is mapped to the operating system’s priority values. The specified thread priority is applied to the thread only while the operation’s main method is executing. It is not applied while the operation’s completion block is executing. For a concurrent operation in which you create your own thread, you must set the thread priority yourself in your custom start method and reset the original priority when the operation is finished.

默认0.5,而且在大量的数据下才会体现出差距,The value you specify is mapped to the operating system’s priority values.它的值其实取决与操作系统的优先级值。

四:线程的状态

总共有五种线程状态,分别为:New(新建),Runnable(就绪),Running(运行),Blocked(阻塞),Dead(死亡).

1.New:当一个线程对象被创建时,它将会处于New这个状态,并处在内存中。

2.Runnable : 当对象执行了start这个对象方法后,就会变为Runnable这个状态,称为i状态,此时系统会将thread这个对象放入可调度线程池中(这个池内也可能包含着其他线程对象!),也可以理解为,只要在这个池内,就可能成为处于Runnable状态或者Running状态。

3.Running: 当CPU调度thread后,它将处于Running状态,注意thread此时仍在池内。但当CPU调度完这个线程并又调度了其他线程时,这个线程就又会变为Runnable的状态,当然仍在池内。

4.Blocked:当这个线程调用了seelp方法或者等待同步锁时,它会从池内移出且处于Blocked状态,但是没有被销毁,仍在内存中,只是不再可调度线程池内。

当seelp到时或者得到同步锁时,线程将又会回到线程池且处于runnable状态。

5.Dead:当线程任务执行完毕或者被强制退出时,就会从池内移出且被在内存中销毁,此时成为处于Dead状态。

2016- 1- 16 NSThread 的学习的更多相关文章

  1. mysql查询练习题-2016.12.16

    >>>>>>>>>> 练习时间:2016.12.16 编辑时间:2016-12-20-->22:12:08 题: 涉及:多表查询.ex ...

  2. 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)

    很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...

  3. Murano Weekly Meeting 2016.08.16

    Meeting time: 2016.August.16 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: ...

  4. 2016.8.16上午纪中初中部NOIP普及组比赛

    2016.8.16上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1334 这次也翻车了,感觉比之前难多了. 辛辛苦苦改完了,太难改 ...

  5. 2016.8.16 JQuery学习记录

    1.$(document).ready(function(){}); 这个函数会在浏览器加载完页面之后,尽快执行: 2.所有的JQuery函数用有个$开始表示,All jQuery functions ...

  6. 2016.8.16 HTML5重要标签及其属性学习

    1.运用BootStrap的基本布局: 2.基本布局第二步: ] 3.BootStrap提供了一个class=”well“”类,可以给你种深度的感觉: 4.不是每一个类都是为了CSS,有些类创建出来只 ...

  7. 2016年3月16日Android学习笔记

    1.Jdk1.7以上switch语句中才能用字符串,在Android Studio中我改正了jdk的版本为1.8,但是还是出同样的错误,原来我用的sdk版本是4.4的,改成5的就没有问题了. 2.引入 ...

  8. 2016.8.16 Java培训第一天

    1. 十进制转换二进制 31/2=15余1  15/2=7余1 7/2=3余1 3/2=1余1    31的二进制结果为11111 35/2=17余1  17/2=8余1  8/2=4余0 4/2=2 ...

  9. ubuntu(16.04.01)学习-day1

    1.修改root用户密码 sudo passwd root 按提示进行设置. 2.从Ubuntu 16.04开始,用户可以实现改变启动器的位置,可以将启动器移到屏幕底部,但是无法移到右边或顶部.打开终 ...

随机推荐

  1. linux笔记:linux常用命令-用户管理命令

    用户管理命令:useradd(添加用户) 用户管理命令:passwd(设置和修改用户密码) 用户管理命令:who(查看所有登录用户的信息)

  2. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  3. python与unicode

    Unicode是一种在计算机上使用的字符编码,是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本转换.处理的要求. Uni ...

  4. Servlet后续的尾(yi)巴--------Filter过滤器

    -------载录自  http://www.blogjava.net/yangaiyou/archive/2007/08/29/140854.html  感谢博主心如止水 一心向佛 描写出这么的具体 ...

  5. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. word文档中查找和替换空格符和回车符

    空格符:^l 回车符:^p

  7. iOS 中对 HTTPS 证书链的验证

    这篇文章是我一边学习证书验证一边记录的内容,稍微整理了下,共扯了三部分内容: HTTPS 简要原理: 数字证书的内容.生成及验证: iOS 上对证书链的验证. HTTPS 概要 HTTPS 是运行在 ...

  8. POC测试——原型验证,降低风险,IT系统销售工作之一

    POC测试,即Proof of Concept,是业界流行的针对客户具体应用的验证性测试,根据用户对采用系统提出的性能要求和扩展需求的指标,在选用服务器上进行真实数据的运行,对承载用户数据量和运行时间 ...

  9. ecshop数据库取数据

    取出所有数据: test_getAll(); function test_getAll() { global $db; $sql = "SELECT user_id, user_name, ...

  10. PHP中的include、include_once、require、require_once

    include.include_once().require.require_once() 作用: 通过 include 或 require 语句,可以将 PHP 文件的内容插入另一个 PHP 文件( ...