随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万
复制代码

 

 

方法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

 

posted on   懂事长qingzZ  阅读(222)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示