当前位置: 移动技术网 > IT编程>移动开发>IOS > 详解iOS多线程之2.NSThread的加锁@synchronized

详解iOS多线程之2.NSThread的加锁@synchronized

2019年07月24日  | 移动技术网IT编程  | 我要评论

简历模版 免费下载,汪洋闳肆,新浪文化读书

那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了。

上代码

声明变量

@interface viewcontroller ()
@property (strong, nonatomic)nsthread *thread1;
@property (strong, nonatomic)nsthread *thread2;
@property (strong, nonatomic)nsthread *thread3;
@property (assign, nonatomic)int lefttickets;
@end

实现代码

- (void)viewdidload {
  [super viewdidload];
  
  self.thread1 = [[nsthread alloc] initwithtarget:self selector:@selector(selltickets) object:nil];
  self.thread2 = [[nsthread alloc] initwithtarget:self selector:@selector(selltickets) object:nil];
  self.thread3 = [[nsthread alloc] initwithtarget:self selector:@selector(selltickets) object:nil];
  self.thread1.name = @"thread1";
  self.thread2.name = @"thread2";
  self.thread3.name = @"thread3";
  // 总票数
  self.lefttickets = 10;
}
// 点击屏幕开启线程
- (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event {
  [self.thread1 start];
  [self.thread2 start];
  [self.thread3 start];
}
- (void)selltickets {
  while (1) {
    @synchronized (self) {
      if (self.lefttickets > 0) {
        [nsthread sleepfortimeinterval:0.2];
        int count = self.lefttickets;
        self.lefttickets = count - 1;
        nslog(@"剩余的票数%d",self.lefttickets);
        nslog(@"当前线程=%@", [nsthread currentthread]);
      }else {
        nslog(@"票卖完了");
        nslog(@"退出线程%@",[nsthread currentthread]);
        [nsthread exit];
      }
    }
  }
}

打印日志

2016-11-04 11:52:25.117 tttttttttt[6753:74162] 剩余的票数9
2016-11-04 11:52:25.117 tttttttttt[6753:74162] 当前线程=<nsthread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:25.393 tttttttttt[6753:74163] 剩余的票数8
2016-11-04 11:52:25.393 tttttttttt[6753:74163] 当前线程=<nsthread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:25.661 tttttttttt[6753:74164] 剩余的票数7
2016-11-04 11:52:25.661 tttttttttt[6753:74164] 当前线程=<nsthread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:25.932 tttttttttt[6753:74162] 剩余的票数6
2016-11-04 11:52:25.933 tttttttttt[6753:74162] 当前线程=<nsthread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.164 tttttttttt[6753:74163] 剩余的票数5
2016-11-04 11:52:26.165 tttttttttt[6753:74163] 当前线程=<nsthread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:26.438 tttttttttt[6753:74164] 剩余的票数4
2016-11-04 11:52:26.439 tttttttttt[6753:74164] 当前线程=<nsthread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:26.704 tttttttttt[6753:74162] 剩余的票数3
2016-11-04 11:52:26.705 tttttttttt[6753:74162] 当前线程=<nsthread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:26.975 tttttttttt[6753:74163] 剩余的票数2
2016-11-04 11:52:26.976 tttttttttt[6753:74163] 当前线程=<nsthread: x608000074540>{number = 4, name = thread2}
2016-11-04 11:52:27.232 tttttttttt[6753:74164] 剩余的票数1
2016-11-04 11:52:27.233 tttttttttt[6753:74164] 当前线程=<nsthread: x608000074580>{number = 5, name = thread3}
2016-11-04 11:52:27.505 tttttttttt[6753:74162] 剩余的票数0
2016-11-04 11:52:27.505 tttttttttt[6753:74162] 当前线程=<nsthread: x608000073880>{number = 3, name = thread1}
2016-11-04 11:52:27.505 tttttttttt[6753:74163] 票卖完了
2016-11-04 11:52:27.506 tttttttttt[6753:74163] 退出线程<nsthread: x608000074540>{number = 4, name = thread2}

我们一般用@synchronized来给线程加锁。它有什么用呢:

(1) 堵塞所在线程,线程里面剩下的任务只有当@synchronized里面的代码执行完毕才能继续往下执行,和队列的同步差不多是一个意思。

(2)当执行@synchronized里面的代码之前,所在线程要先检查是否有其他的线程执行里面的代码。如果没有,才继续往下执行。

再看打印日志里面最后一条,说明了只有线程“thread3”退出了,其他的线程没有退出。

我上篇文章讲,不用管线程的退出,任务执行完线程会自动退出。但是这是一个while循环啊!如果不退出线程,线程会一直执行。

- (void)selltickets {
  while (1) {
    @synchronized (self) {
      if (self.lefttickets > 0) {
        [nsthread sleepfortimeinterval:0.2];
        int count = self.lefttickets;
        self.lefttickets = count - 1;
        nslog(@"剩余的票数%d",self.lefttickets);
        nslog(@"当前线程=%@", [nsthread currentthread]);
      }else {
        nslog(@"票卖完了");
        nslog(@"退出线程%@",[nsthread currentthread]);
        // 不让线程退出
        //[nsthread exit];
      }
    }
  }
}

打印的日志

2016-11-04 12:01:53.309 tttttttttt[7110:78974] 当前线程=<nsthread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.556 tttttttttt[7110:78973] 剩余的票数0
2016-11-04 12:01:53.556 tttttttttt[7110:78973] 当前线程=<nsthread: x600000076fc0>{number = 3, name = thread1}
2016-11-04 12:01:53.556 tttttttttt[7110:78975] 票卖完了
2016-11-04 12:01:53.557 tttttttttt[7110:78975] 退出线程<nsthread: x600000077240>{number = 5, name = thread3}
2016-11-04 12:01:53.558 tttttttttt[7110:78974] 票卖完了
2016-11-04 12:01:53.559 tttttttttt[7110:78974] 退出线程<nsthread: x600000076f40>{number = 4, name = thread2}
2016-11-04 12:01:53.560 tttttttttt[7110:78973] 票卖完了

那又为什么只有线程thread2退出呢?(注:每次退出的线程是不确定的)因为当线程thread2退出了,并没有执行完@synchronized里的方法,线程thread1和线程thread3还在等thread2执行完了,它们好去执行呢。但是线程thread2已经死了,不可能再执行了。这就造成线程thread1和线程thread3一直都在内存里,没有被退出,造成了cpu不必要的开销,所以我们最好不要在@synchronized里面退出线程。

- (void)selltickets {
  while (1) {
    @synchronized (self) {
      if (self.lefttickets > 0) {
        [nsthread sleepfortimeinterval:0.2];
        int count = self.lefttickets;
        self.lefttickets = count - 1;
        nslog(@"剩余的票数%d",self.lefttickets);
        nslog(@"当前线程=%@", [nsthread currentthread]);
      }
    }
    if (self.lefttickets == 0) {
      nslog(@"票卖完了");
      nslog(@"退出线程%@",[nsthread currentthread]);
      [nsthread exit];
    }
  }
}

2016-11-04 12:06:51.795 tttttttttt[7295:81206] 票卖完了
2016-11-04 12:06:51.795 tttttttttt[7295:81207] 票卖完了
2016-11-04 12:06:51.795 tttttttttt[7295:81208] 票卖完了
2016-11-04 12:06:51.796 tttttttttt[7295:81206] 退出线程<nsthread: x60000026a3c0>{number = 3, name = thread1}
2016-11-04 12:06:51.796 tttttttttt[7295:81207] 退出线程<nsthread: x60000026a380>{number = 4, name = thread2}
2016-11-04 12:06:51.796 tttttttttt[7295:81208] 退出线程<nsthread: x60000026a740>{number = 5, name = thread3}

这就是nsthread加锁以及加锁的一些注意事项。如果感觉对你有用,记得关注啊,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网