欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-21 14:40:16
那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了。 上代码 声明变量 @interface viewcontroller ()...

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

上代码

声明变量

@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加锁以及加锁的一些注意事项。如果感觉对你有用,记得关注啊,也希望大家多多支持。

上一篇:

下一篇: