方法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. [UE4]代理事件(C++)

    用宏定义类似格式: DECLARE_DELEGATE //普通代理 DECLARE_DYNAMIC_DELEGATE_TwoParams //动态代理 DECLARE_DYNAMIC_MULTICAS ...

  2. phpexcel导入数据出现PHPExcel_RichText Object解决办法

    在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...

  3. 取得 Ajax 返回参数

    ajax 函数,p1 为正常参数 function ExecWebFunction(callback,p1) { $.ajax({ type: "POST", contentTyp ...

  4. javascript节点操作insertBefor()

    如果想要把节点放在某个特定的位置,而不是放在末尾,就可以使用insertBefore(a,b) 参数a:要插入的节点 参数b:作为参照的节点. var oDiv = document.getEleme ...

  5. 6.13-C3p0连接池配置,DBUtils使用

    DBCP连接池 一.C3p0连接池配置 开源的JDBC连接池 使用连接池的好处: 减轻数据库服务器压力 数据源: ComboPooledDataSource ComboPooledDataSource ...

  6. express基础

    express框架 const express = require("express"); 引入express框架 var app= express(); 实例化   相当于构造函 ...

  7. corejava-内容梳理

  8. UVA327

    模拟 这个问题的任务是求解一组c语言里的表达式,但是你不需要知道c语言是怎么解决这个问题!每一行一个表达式,每个表达式的组成不会超过110个字符.待求解的表达式只包含一个int类型变量和一个组有限的操 ...

  9. 0_Simple__simpleVoteIntrinsics + 0_Simple__simpleVoteIntrinsics_nvrtc

    介绍了线程束表决函数的实例(其概念介绍见 http://www.cnblogs.com/cuancuancuanhao/p/7841512.html),并在静态和运行时编译两种条件下进行使用. ▶ 源 ...

  10. leetcode453

    public class Solution { public int MinMoves(int[] nums) { var list = nums.OrderBy(x => x).ToList( ...