2016- 1- 16 NSThread 的学习
一: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 的学习的更多相关文章
- mysql查询练习题-2016.12.16
>>>>>>>>>> 练习时间:2016.12.16 编辑时间:2016-12-20-->22:12:08 题: 涉及:多表查询.ex ...
- 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)
很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...
- Murano Weekly Meeting 2016.08.16
Meeting time: 2016.August.16 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: ...
- 2016.8.16上午纪中初中部NOIP普及组比赛
2016.8.16上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1334 这次也翻车了,感觉比之前难多了. 辛辛苦苦改完了,太难改 ...
- 2016.8.16 JQuery学习记录
1.$(document).ready(function(){}); 这个函数会在浏览器加载完页面之后,尽快执行: 2.所有的JQuery函数用有个$开始表示,All jQuery functions ...
- 2016.8.16 HTML5重要标签及其属性学习
1.运用BootStrap的基本布局: 2.基本布局第二步: ] 3.BootStrap提供了一个class=”well“”类,可以给你种深度的感觉: 4.不是每一个类都是为了CSS,有些类创建出来只 ...
- 2016年3月16日Android学习笔记
1.Jdk1.7以上switch语句中才能用字符串,在Android Studio中我改正了jdk的版本为1.8,但是还是出同样的错误,原来我用的sdk版本是4.4的,改成5的就没有问题了. 2.引入 ...
- 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 ...
- ubuntu(16.04.01)学习-day1
1.修改root用户密码 sudo passwd root 按提示进行设置. 2.从Ubuntu 16.04开始,用户可以实现改变启动器的位置,可以将启动器移到屏幕底部,但是无法移到右边或顶部.打开终 ...
随机推荐
- IOC Container(服务容器)的工作机制
IOC Container 是laravel的一个核心内容,有了IOC Container在Laravel的强大表现,我们可以在Laravel中实现很大程度的代码维护性.(文档我是看的懵逼懵逼的(*^ ...
- 【软件使用】TortoiseSVN版本管理软件使用简单说明
TortoiseSVN版本管理软件使用简单说明 很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器.那么TortoiseSVN就非常适合. 第一步:下载Tortois ...
- .NET简谈反射(动态调用)
我们继续C#基础知识的学习,这篇文章主要要讲的是我们C#程序员迈向高级C#程序员的关键性的一步. 有的朋友会说事实不是这样的,我不用反射就不能开发吗?当然可以,但是用与不用肯定是不一样的,任何复杂抽象 ...
- getsockname和getpeername
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen); // 获取与某个套接字关联的本地协议地址 i ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hduoj-----(1068)Girls and Boys(二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Java初学之华容道游戏
package hhuarongdao; public class example { public static void main(String args[]) { new Hua_Rong_Ro ...
- 用Visual C#向access添加数据
(1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...
- div中的字符换行
div中的字符换行 转载自:http://blog.sina.com.cn/s/blog_6a79bc480100tizi.html 1.强制不换行,同时以省略号结尾. <div style ...
- 今天遇到一个问题,linq语句的写法,查询动态字段
public List<Location> getLocationList(int companyid, string searchValue, string searchField) ...