一: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. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  2. python中的异常处理

    主要用到 try...except...raise...finally... 1. try...except... try: for i in range(1, 1000): print i time ...

  3. hdu----(1677)Nested Dolls(DP/LIS(二维))

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)

    假设有一个基于.Net的Web Service,其名称为SaveProduct POST /ProductService.asmx HTTP/1.1 Host: localhost Content-T ...

  5. JDE报表开发笔记(R5537011 收货校验统计表)

    业务场景:根据批次收货,收货后对该批次产品进行检验,记录检验结果生成统计表. 涉及表:主表F37011,业务从表F43121/F4101/F4108 ------------------------- ...

  6. YUM安装提示PYCURL ERROR 6 - "Couldn't错误的解决办法

    当编译PHP时出现如下错误时,找不到头绪 这时,打开DNS   vim /etc/resolv.conf   添加一行nameserver 192.168.1.1 完成上一步,则解决该问题 或者:ec ...

  7. php中curl和fsockopen发送远程数据的应用

    最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起. 这是简单的将一个变量post到另外一个页面 $url = ''; $d ...

  8. 项目构建工具Gradle的使用入门(参考,只表明地址)

    Gradle入门介绍:简介 http://blog.jobbole.com/71999/ Gradle入门介绍:第一个Java项目 http://blog.jobbole.com/72558/ Gra ...

  9. CentOS Hadoop格式化HDFS异常java.net.UnknownHostException

    #bin/hadoop namenode -format DEPRECATED: Use of this script to execute hdfs command is deprecated. I ...

  10. eclipse ee 装oepe(Oracle Enterprise Pack for Eclipse)插件

    eclipse j2ee 安装weblogic插件Oracle Enterprise Pack for Eclipse(oepe) eclipse j2ee已经集成了常见的几个服务器容器,比如tomc ...