一: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. Timer与TimerTask的真正原理&使用介绍

    转载: Timer与TimerTask的真正原理&使用介绍 其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来 ...

  2. 关于ASP.NET与CLR相互关系的一些总结

    原文地址:http://www.cnblogs.com/jasenkin/archive/2010/10/20/asp-net-clr-relation.html CLR(COM服务器) CLR作为一 ...

  3. robotframework笔记23

    远程库接口 远程库接口提供了对在测试库 比机器人框架本身是在不同的机器上运行, 同时实现图书馆使用其他语言比 本机支持Python和Java. 为一个测试库用户远程 library看起来几乎一样的其他 ...

  4. Ubuntu 修复windows启动项

    打开终端输入命令sudo gedit /etc/default/grub修改GRUB_TIMEOUT="10"然后在终端中输入sudo update-grubupdate 命令会自 ...

  5. p标签中的span标签文字垂直居中对齐

    <p>轻舞飞扬<span>第一次亲密接触</span></p> p标签的font-size:30px; span标签的font-size:24px; 让 ...

  6. C++继承与派生(原理归纳)

    1.   C++继承与java不同,java遵循单继承,但java的接口为其不足做了很好的弥补了. C++则是灵活的多,为多继承.即一个C++类可以同时继承N个类的属性. 2. 对于继承方式 : 有三 ...

  7. 九度 题目1437:To Fill or Not to Fill

    题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...

  8. Java面试题(1)- 高级特性

    1. The diffrence between java.lang.StringBuffer and java.lang.StringBuilder? java.lang.StringBuffer: ...

  9. CentOS查看软件源提供的软件版本命令

    yum list available第二列包更详细的说明:yum info packagename

  10. Linux查看程序端口占用情况【转】

    今天发现服务器上Tomcat 8080端口起不来,老提示端口已经被占用. 使用命令: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat ...