线程

1、多线程--单个应用程序内的多个代码执行路径

线程支持:run loop 、同步工具、线程间通信以及线程包。

线程包:{ NSThread :

[NSThread detachNewThreadSelector:@selector(testForOne:) toTarget:self withObject:@"qire"];    立即创建一个线程

NSThread *myThread = [[NSThread alloc]initWithTarget:self selector:@selector(testForTwo:) object:@"我是一个大企鹅"];

[myThread start];  并不会立即创建一个线程,需要手动启动

以上两个当线程退出时,资源由系统自动回收,之后不需要在其他线程中显式链接--????

[self performSelectorOnMainThread:@selector(sendMessageToThread:) withObject:@"我给你发送了一条消息" waitUntilDone:YES];  在当前的线程中执行任务,不新建

waitUntilDone:当前线程跟目标线程一致,设置yes:在当前线程立即执行消息

no:则在当前线程的run loop中排队等待执行。

[self performSelectorInBackground:@selector(sendMessageToThread:) withObject:@"我在后台运行了吗?”];    会在后台, 新建一个线程

[self performSelector:@selector(sendMessageToThread:) withObject:@"我延时了5秒" afterDelay:5.0f];    该方法的执行过程,等延时时间条件满足时,当前线程才尝试将该消息加入run loop 排队,准备执行 }

2、一个线程需要管理代码的数据结构组合--内核级与应用数据级

3、线程的并发---每个新的执行路径独立于main,一个个独立的线程

4、线程状态:running\ready\blocked

5、创建新的线程--指定入口函数(the code you want to do)

6、run loop 为线程监测 事件源,事件到达--调度事件到run loop--分配给指定程序

7、配置run loop:启动线程-获取run loop引用对象-设置事件处理程序-运行run loop

8、同步工具:原子操作、内存屏障、volatile变量、锁、条件、selector例程

9、线程通信:

Distributed objects is much more suitable for communicating with other processes, where the overhead of going between processes is already high.

Message queues:simple and convenient, they are not as efficient as some other communications techniques

direct message: automatically serialized on that thread.

condition: gate keeper

run loop: 事件驱动

port and socket: a more elaborate way to communication between two threads, but it is also a very reliable technique. More importantly, ports and sockets can be used to communicate with external entities, such as other processes and services. For efficiency, ports are implemented using run loop sources, so your thread sleeps when there is no data waiting on the port.

线程间通信、共享数据结构、线程退出时的行为、处理异常、中断线程

一个线程中默认都包含有一个run loop

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 线程、进程 1.iOS中的多线程操作、多线程方式? 2.多线程的优点和缺点分别是什么? 答:优点:1、将耗时较长的...
    丶逐渐阅读 1,411评论 0 8
  • 本文选译自《Threading Programming Guide》。 导语 线程技术作为在单个应用程序中并发执行...
    巧巧的二表哥阅读 2,475评论 4 24
  • 一、前言 上一篇文章iOS多线程浅汇-原理篇中整理了一些有关多线程的基本概念。本篇博文介绍的是iOS中常用的几个多...
    nuclear阅读 2,075评论 6 18
  • 前言 多线程的价值无需赘述,对于App性能和用户体验都有着至关重要的意义,在iOS开发中,Apple提供了不同的技...
    Crazy2015阅读 556评论 0 1
  • 资源网址http://www.2mn.tv/ 恋恋影视http://izzs.cc/ 涨姿势ht...
    北方蜘蛛阅读 7,341评论 0 0