OC 线程操作2 - NSThread
方法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的更多相关文章
- OC 线程操作 - GCD队列组
1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...
- OC 线程操作 - GCD快速迭代
- (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...
- OC 线程操作3 - NSOperation
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...
- OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...
- OC 线程操作1 - pthread
#import "ViewController.h" #import <pthread.h> //1.需要包含这个头文件 @interface ViewControll ...
- OC 线程操作3 - NSOperation 实现线程间通信
#import "ViewController.h" @interface ViewController () /** 图片 */ @property (weak, nonatom ...
- OC 线程操作 - GCD使用 - 栅栏函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...
- OC线程操作-GCD介绍
1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.
随机推荐
- Goroutine(协程)为何能处理大并发?
简单来说:协程十分轻量,可以在一个进程中执行有数以十万计的协程,依旧保持高性能. 进程.线程.协程的关系和区别: 进程拥有自己独立的堆和栈,既不共享堆,亦不共享栈,进程由操作系统调度. 线程拥有自己独 ...
- java中的URLConnection和HttpURLConnection有什么区别(因为我自己搜到别人写的区别看下来都没有什么区别)
今天看了一下公司同事的代码,如下 URLConnection connection = openConnection(localURL); HttpURLConnection httpURLConne ...
- Flask 进阶二
flask中的路由系统: endpoint:反向url地址,默认为视图函数名(url_for). from flask import Flask,url_for app = Flask(__name_ ...
- 表单:提交验证,及blur事件验证
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 前台框架vue.js中怎样嵌入 Echarts 组件?
目前常用的图标插件有 charts, Echarts, highcharts.这次将介绍 Echarts 在 Vue 项目中的应用. 一.安装插件 使用 cnpm 安装 Echarts cnpm i ...
- 进程和创建线程的两种方法(threading.Thread)
进程 如QQ 要以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的管理, 网络接口的调用等,进程就是各种资源管理的集合 线程:是操作系统最小的调度单位,是一串指令的结合 进程 要操作 ...
- Spring cron 定时调度配置
IDEA 或者 STS http://spring.io/guides/gs/scheduling-tasks/ spring mvc : 结构: Seconds Minutes Hours Day ...
- Linux下类似windows下_beginthread和_endthread 的多线程开发
在 windows下头文件中包含 #include<process.h> 就可以使用_beginthread进行线程创建.个人感觉挺方便的. 在linux下类似于_beginthread ...
- ready 事件 DOM(文档对象模型) 已经加载....
定义和用法 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. 由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置于该事件中是非 ...
- ssh反向连接内网主机
holer听别人说也挺好用不过本人没试过:https://github.com/Wisdom-Projects/holer 利用autossh建立稳定隧道,前提双方互加公钥信任. # yum inst ...