IOS---NSThread同步处理
发布日期:2021-11-21 04:40:59
浏览次数:40
分类:技术文章
本文共 762 字,大约阅读时间需要 2 分钟。
为了同步可以用下面的代码
- (void)viewDidLoad{ num = 100; NSThread *thread1 =[[NSThread alloc]initWithTarget:self selector:@selector(showData) object:nil]; [thread1 setName:@"thread--1"]; NSThread *thread2 =[[NSThread alloc]initWithTarget:self selector:@selector(showData) object:nil]; [thread2 setName:@"thread--2"]; [thread1 start]; [thread2 start]; lock = [[NSLock alloc]init]; }
-(void)showData{ while (TRUE) { [lock lock]; if(num > 0) { [NSThread sleepForTimeInterval:0.3]; NSLog(@"当前%d,线程名:%@",num,[[NSThread currentThread] name]); num--; } else { break; } [lock unlock]; }}结果是不会为-1的
转载地址:https://blog.csdn.net/xiaoji94shen/article/details/20997097 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月01日 19时52分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LeetCode题解(1072):按列翻转得到最大值等行数(Python)
2019-04-26
LeetCode题解(1086):前五科的均分(Python)
2019-04-26
LeetCode题解(1364):顾客的可信联系人数量(SQL)
2019-04-26
LeetCode题解(1369):获取最近第二次的活动(SQL)
2019-04-26
LeetCode题解(1378):使用唯一标识码替换员工ID(SQL)
2019-04-26
LeetCode题解(1384):按年度列出销售总额(SQL)
2019-04-26
LeetCode题解(1393):股票的资本损益(SQL)
2019-04-26
LeetCode题解(1398):购买了产品A和产品B却没有购买产品C的顾客(SQL)
2019-04-26
LeetCode题解(1407):排名靠前的旅行者(SQL)
2019-04-26
LeetCode题解(1412):查找成绩处于中游的学生(SQL)
2019-04-26
LeetCode题解(1421):净现值查询(SQL)
2019-04-26
LeetCode题解(1435):制作会话柱状图(SQL)
2019-04-26
LeetCode题解(1440):计算布尔表达式的值(SQL)
2019-04-26
LeetCode题解(1061):按字典序排列最小的等效字符串(Python)
2019-04-26
LeetCode题解(1101):彼此熟识的最早时间(Python)
2019-04-26
LeetCode题解(1102):得分最高的路径(Python)
2019-04-26
LeetCode题解(1135):最低成本联通所有城市(Python)
2019-04-26
LeetCode题解(1168):水资源分配优化(Python)
2019-04-26
LeetCode题解(1724):检查边长度限制的路径是否存在II(Python)
2019-04-26
文件服务器实现基本条件!
2019-04-26