方法1 :直接创建 alloc init
- (void)createNSThread111{
/*   
参数1: (nonnull id) 目标对象 self
参数2:(nonnull SEL) 方法选择器 ,调用的方法
参数3:(nullable id) 前面调用方法需要传递的参数 nil *
//1.创建线程 NSThread *thread= [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"abc"];
//2.开启线程 [thread start]; } - (void)run:(NSString *)pama{ NSLog(@"---fun---%@", [NSThread currentThread]); }
打印结果: 2018-06-22 14:10:57.529875+0800 线程操作[6518:200227] ---fun---<NSThread: 0x608000265e40>{number = 3, name = (null)}--abc
 
 
方法2. 分离子线程 ,自动启动线程 detach [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分离子线程"];
打印结果 : 2018-06-22 14:10:57.530121+0800 线程操作[6518:200228] ---fun---<NSThread: 0x608000265f40>{number = 4, name = (null)}--分离子线程
 
方法3.开启后台一个线程 performSelectorInBackground
[self performSelectorInBackground:@selector(run:) withObject:@"开启一后台线程"];
打印结果 : 2018-06-22 14:10:57.530164+0800 线程操作[6518:200229] ---fun---<NSThread: 0x608000265dc0>{number = 5, name = (null)}
 
--开启一后台线程
第一种 设置线程阻塞,阻塞2秒
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;// 线程停止 几秒
[NSThread sleepForTimeInterval:2.0];
 
第二种设置线程阻塞2,以当前时间为基准阻塞4秒
+ (void)sleepUntilDate:(NSDate *)date;
//控制线程状态
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
[NSThread sleepUntilDate:date];
 
// 线程退出
+ (void)exit;   

他人总结 OS开发多线程篇—线程的状态 链接 :https://www.cnblogs.com/wendingding/p/3807184.html

 







OC 线程操作2 - NSThread的更多相关文章

  1. OC 线程操作 - GCD队列组

    1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...

  2. OC 线程操作 - GCD快速迭代

    - (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...

  3. OC 线程操作3 - NSOperation

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  4. OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...

  5. OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...

  6. OC 线程操作1 - pthread

    #import "ViewController.h" #import <pthread.h> //1.需要包含这个头文件 @interface ViewControll ...

  7. OC 线程操作3 - NSOperation 实现线程间通信

    #import "ViewController.h" @interface ViewController () /** 图片 */ @property (weak, nonatom ...

  8. OC 线程操作 - GCD使用 - 栅栏函数

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...

  9. OC线程操作-GCD介绍

    1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.

随机推荐

  1. Vcenter 和ESXi License过期解决办法

    Vcenter License过期解决办法 用client连接vcenter:  KEY必须用vCenter Server 5 Standard序列号如果使用使用的“vCenter Server 5 ...

  2. web项目除了业务还需要关注的点

    1:安全性,不允许访问外网,访问外网通过反向代理的方式. 2:安全性,和外网交互的时候,需要CA证书,基于SSL协议的证书 3:日志,生产上通常会关闭某些日志,所以,允许出现的日志就显得至关重要了. ...

  3. javascript的事件流

    事件流包括三个阶段: 1.事件捕获阶段 2.处于目标阶段 3.事件冒泡阶段 1.事件捕获阶段 现在页面中有一个按钮. 如果单击这个按钮的话,在事件捕获过程中,document会首先接收到click事件 ...

  4. javascript的焦点管理

    HTML5也添加了辅助管理DOM焦点的功能. 元素获得焦点的方式有页面加载,用户输入和代码中调用的focus()方法. 而document.activeElement属性保存着当前获得焦点的引用. v ...

  5. javascript创建对象之构造函数模式(二)

    对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...

  6. HP-UX oracle RAC 双机实践 (转载)

    一.软硬件配置检查 1.检查内存#/usr/contrib/bin/machinfoCPU info:  4 Intel(R) Itanium 2 9100 series processors (1. ...

  7. Android如何使用Https

    什么是Https? HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...

  8. 包与常用模块:time,sys。

    一  包的初识: 首先包在pycharm中的表现形式为packa文件夹:在python3种那么我们创建一个packa时会发现下边会自动跟一个—init—.py文件 包的定义:包就是一个包含有——ini ...

  9. url的命名与反向解析

    url命名和反向解析  1. 命名   # url(r'^press_list/$', views.press_list,name='press_list'),     url(r'^pre/$', ...

  10. snmp简单识记

    免费snmp探测 http://webluker.com/webtools/snmp snmp简单网络管理协议(simple network management protocol)前身时sgmp简单 ...