Detailed IOS lock @synchronized_ios for multi-thread threads 2.NSThread

Source: Internet
Author: User
Tags ticket

When you need to lock it, that is, when more than one thread at the same time to operate a variable, you need to add a lock.

On the Code

declaring variables

@interface Viewcontroller ()
@property (Strong, nonatomic) Nsthread *thread1;
@property (Strong, nonatomic) Nsthread *thread2;
@property (Strong, nonatomic) Nsthread *thread3;
@property (assign, nonatomic) int lefttickets;
@end

Implementation code

-(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";
Total number of votes self.lefttickets = 10;
  //Click on screen Open thread-(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 SL
        eepfortimeinterval:0.2];
        int count = Self.lefttickets;
        Self.lefttickets = count-1;
        NSLog (@ "The remaining number of votes%d", self.lefttickets);
      NSLog (@ "Current thread =%@", [Nsthread CurrentThread]); }else {NSLOG (@ "ticket sold out");
        NSLog (@ "Exit thread%@", [Nsthread CurrentThread]);
      [Nsthread exit];
 }
    }
  }
}

Print Log

2016-11-04 11:52:25.117 tttttttttt[6753:74162] The remaining number of votes 9
2016-11-04 11:52:25.117 tttttttttt[6753:74162] Current thread =<nsthread:x608000073880>{number = 3, name = Thread1}
2016-11-04 11:52:25.393 tttttttttt[6753:74163] The remaining number of votes 8
2016-11-04 11:52:25.393 tttttttttt[6753:74163] Current thread =<nsthread:x608000074540>{number = 4, name = Thread2}
2016-11-04 11:52:25.661 tttttttttt[6753:74164] The remaining number of votes 7
2016-11-04 11:52:25.661 tttttttttt[6753:74164] Current thread =<nsthread:x608000074580>{number = 5, name = Thread3}
2016-11-04 11:52:25.932 tttttttttt[6753:74162] The remaining number of votes 6
2016-11-04 11:52:25.933 tttttttttt[6753:74162] Current thread =<nsthread:x608000073880>{number = 3, name = Thread1}
2016-11-04 11:52:26.164 tttttttttt[6753:74163] The remaining number of votes 5
2016-11-04 11:52:26.165 tttttttttt[6753:74163] Current thread =<nsthread:x608000074540>{number = 4, name = Thread2}
2016-11-04 11:52:26.438 tttttttttt[6753:74164] The remaining number of votes 4
2016-11-04 11:52:26.439 tttttttttt[6753:74164] Current thread =<nsthread:x608000074580>{number = 5, name = Thread3}
2016-11-04 11:52:26.704 tttttttttt[6753:74162] The remaining number of votes 3
2016-11-04 11:52:26.705 tttttttttt[6753:74162] Current thread =<nsthread:x608000073880>{number = 3, name = Thread1}
2016-11-04 11:52:26.975 tttttttttt[6753:74163] The remaining number of votes 2
2016-11-04 11:52:26.976 tttttttttt[6753:74163] Current thread =<nsthread:x608000074540>{number = 4, name = Thread2}
2016-11-04 11:52:27.232 tttttttttt[6753:74164] The remaining number of votes 1
2016-11-04 11:52:27.233 tttttttttt[6753:74164] Current thread =<nsthread:x608000074580>{number = 5, name = Thread3}
2016-11-04 11:52:27.505 tttttttttt[6753:74162] The remaining number of votes 0
2016-11-04 11:52:27.505 tttttttttt[6753:74162] Current thread =<nsthread:x608000073880>{number = 3, name = Thread1}
2016-11-04 11:52:27.505 tttttttttt[6753:74163] tickets sold OUT
2016-11-04 11:52:27.506 tttttttttt[6753:74163] Exit thread <nsthread:x608000074540>{number = 4, name = Thread2}

We usually use @synchronized to give the line Cheng. What's the use of it:

(1) Plug the thread, the rest of the task only when the @synchronized inside the code to continue to execute, and the synchronization of the queue is almost a meaning.

(2) Before executing the code inside the @synchronized, the thread must first check if there are other threads executing the code inside. If not, proceed down.

Look at the last article in the print log, which shows that only the thread "thread3" exits, and the other threads do not exit.

My last article said that without the exit of the pipeline, the thread will exit automatically when the task finishes executing. But this is a while loop! If the thread is not rolled back, it is always executed.

-(void) Selltickets {while
  (1) {
    @synchronized (self) {
      if (Self.lefttickets > 0) {
        [Nsthread SLEEPFO rtimeinterval:0.2];
        int count = self.lefttickets;
        Self.lefttickets = count-1;
        NSLog (@ "The remaining number of votes%d", self.lefttickets);
        NSLog (@ "Current thread =%@", [Nsthread CurrentThread]);
      } else {
        NSLog (@ "ticket sold Out");
        NSLog (@ "Exit thread%@", [Nsthread CurrentThread]);
        Do not allow threads to exit
        //[nsthread exit];}}

Printed log

2016-11-04 12:01:53.309 tttttttttt[7110:78974] Current thread =<nsthread:x600000076f40>{number = 4, name = Thread2}
2016-11-04 12:01:53.556 tttttttttt[7110:78973] The remaining number of votes 0
2016-11-04 12:01:53.556 tttttttttt[7110:78973] Current thread =<nsthread:x600000076fc0>{number = 3, name = Thread1}
2016-11-04 12:01:53.556 tttttttttt[7110:78975] tickets sold OUT
2016-11-04 12:01:53.557 tttttttttt[7110:78975] Exit thread <nsthread:x600000077240>{number = 5, name = Thread3}
2016-11-04 12:01:53.558 tttttttttt[7110:78974] tickets sold OUT
2016-11-04 12:01:53.559 tttttttttt[7110:78974] Exit thread <nsthread:x600000076f40>{number = 4, name = Thread2}
2016-11-04 12:01:53.560 tttttttttt[7110:78973] tickets sold OUT

So why only the thread thread2 out? (Note: Each exit thread is indeterminate) because when the thread thread2 exits and does not finish executing the @synchronized method, thread thread1 and thread thread3 are still waiting for thread2 to execute, they are ready to execute. But the thread thread2 is dead and impossible to execute. This causes thread thread1 and thread thread3 to be in memory all the time, have not been exited, caused the CPU unnecessary overhead, so we had better not exit the thread in @synchronized.

 

2016-11-04 12:06:51.795 tttttttttt[7295:81206] tickets sold OUT
2016-11-04 12:06:51.795 tttttttttt[7295:81207] tickets sold OUT
2016-11-04 12:06:51.795 tttttttttt[7295:81208] tickets sold OUT
2016-11-04 12:06:51.796 tttttttttt[7295:81206] Exit thread <nsthread:x60000026a3c0>{number = 3, name = Thread1}
2016-11-04 12:06:51.796 tttttttttt[7295:81207] Exit thread <nsthread:x60000026a380>{number = 4, name = Thread2}
2016-11-04 12:06:51.796 tttttttttt[7295:81208] Exit thread <nsthread:x60000026a740>{number = 5, name = Thread3}

This is the Nsthread lock and lock some of the considerations. If the feeling is useful to you, remember to pay attention to Ah, I also hope that we support the cloud-dwelling community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.