线程同步的两种防护四
来源:互联网 发布:小猪cms微店系统源码 编辑:程序博客网 时间:2024/04/25 11:44
#import "TRViewController.h"
@interface TRViewController ()
@property (nonatomic)int ticketsCount;
@property (nonatomic)int selledCount;
@property (nonatomic, strong)NSLock *mylock;
@end
@implementation TRViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.ticketsCount =100;
self.mylock = [[NSLockalloc]init];
NSThread *t1 = [[NSThreadalloc]initWithTarget:selfselector:@selector(beginSell) object:Nil];
t1.name = @"一号窗口";
[t1 start];
NSThread *t2 = [[NSThreadalloc]initWithTarget:selfselector:@selector(beginSell) object:Nil];
t2.name = @"二号窗口";
[t2 start];
}
-(void)beginSell{
while (YES) {
//同步代码块任意对象都有一个锁的标示(锁旗标)
// 2
@synchronized(self)
{
// [self.mylock lock];
NSString *name = [NSThreadcurrentThread].name;
NSLog(@"%@开始卖票",name);
[NSThreadsleepForTimeInterval:1];
self.selledCount++;
NSLog(@"%@卖票结束卖了%d号票还剩%d张",name,self.selledCount,self.ticketsCount-self.selledCount);
}
// [self.mylock unlock];
}
}
- 线程同步的两种防护四
- 线程的两种同步方式测试
- 线程(Thread)同步的两种表现形式
- 四种线程同步
- C++线程同步的四种方式
- C++ 线程同步的四种方式
- Windows线程同步的四种方法
- 线程同步两种方式
- 线程的同步(四)---信号量
- 四、线程的同步问题
- 线程安全 线程同步两种方法
- 线程同步四种方式
- 线程之同步的两种条件总结
- 线程通讯和同步的两种实现方法
- Java中线程同步的两种方式
- 线程通讯和同步的两种实现方…
- 线程同步的四种方式(MFC线程同步4种方式)
- 线程同步的四种方式(MFC线程同步4种方式)
- HTTP 协议详解
- asp.net中弹出层效果(div,不可拖拉)
- View的setOnClickListener的添加方法
- 深入研究java.lang.ThreadLocal类
- Github for Windows使用图文教程
- 线程同步的两种防护四
- C++11中值得关注的几大变化
- 下层设备以及虚拟设备造成的环路不容忽视
- class file editor: source not found
- MySQl 子查询,左右连接,多表连接学习笔记
- Uboot 2014新版的代码路径
- UVA 10465 Homer Simpson 完全背包
- 判断手机联网类型
- 2437比较大小类模板