互斥锁(同步锁):有效的防止因多线程抢夺资源造成的数据安全问题.因为线程等待,需要消耗大量的CPU资源.

线程同步:指线程在同一条线上执行(按顺序的执行任务)

 self.tickets = 1000;

    self.th1 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTickets:) object:nil];

    self.th1.name = @"小丽";

    [self.th1 start];

    

     self.th2 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTickets:) object:nil];

    self.th2.name = @"校长";

    [self.th2 start];

    

     self.th3 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTickets:) object:nil];

    self.th3.name = @"小明";

    [self.th3 start];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)sellTickets:(id)obj{

    while (self.tickets > 0) {

        @synchronized (self) {

            if (self.tickets >0) {

                 NSLog(@"%@ 买了一张票,还剩%ld张",[NSThread currentThread].name, --self.tickets );

            }else{

                NSLog(@"票卖完了");

            }       

        } 

    }

}