一.创建
NSThread的两种创建方法:
//方法1
NSThread *thread = [[NSThread alloc] initWithTarget:<#(nonnull id)#> selector:<#(nonnull SEL)#> object:<#(nullable id)#>];
//方法2
[NSThread detachNewThreadSelector:<#(nonnull SEL)#> toTarget:<#(nonnull id)#> withObject:<#(nullable id)#>]
区别在于使用方法1中的创建方式,需要通过start来启动线程,如下:
threadOne = [[NSThread alloc] initWithTarget:self selector:@selector(addCount) object:nil];
threadOne.name = @"我的线程1";//设置线程的名称
[threadOne start];
方法2中的创建方式会直接启动线程。
二.线程同步锁
1.NSLock
- (void)viewDidLoad {
[super viewDidLoad];
count = 0;
lock = [[NSLock alloc] init];
threadOne = [[NSThread alloc] initWithTarget:self selector:@selector(addCount) object:nil];
threadOne.name = @"我的线程1";
[threadOne start];
threadTwo = [[NSThread alloc] initWithTarget:self selector:@selector(addCount) object:nil];
threadTwo.name = @"我的线程2";
[threadTwo start];
[NSThread detachNewThreadSelector:@selector(addCount) toTarget:self withObject:nil];
}
-(void)addCount
{
while (1) {
[lock lock];
[NSThread sleepForTimeInterval:0.1];
count++;
NSLog(@"count=%ld,当前线程:%@",count,[NSThread currentThread].name);
[lock unlock];
}
}
执行以上代码的结果是:
**2016-02-25 17:10:46.106 multiThreadTest[3413:3631691] count=1,****当前线程:我的线程****2**
**2016-02-25 17:10:46.209 multiThreadTest[3413:3631692] count=2,****当前线程:**
**2016-02-25 17:10:46.310 multiThreadTest[3413:3631690] count=3,****当前线程:我的线程****1**
**2016-02-25 17:10:46.413 multiThreadTest[3413:3631691] count=4,****当前线程:我的线程****2**
**2016-02-25 17:10:46.518 multiThreadTest[3413:3631692] count=5,****当前线程:**
**2016-02-25 17:10:46.622 multiThreadTest[3413:3631690] count=6,****当前线程:我的线程****1**
**2016-02-25 17:10:46.727 multiThreadTest[3413:3631691] count=7,****当前线程:我的线程****2**
**2016-02-25 17:10:46.831 multiThreadTest[3413:3631692] count=8,****当前线程:**
**2016-02-25 17:10:46.936 multiThreadTest[3413:3631690] count=9,****当前线程:我的线程****1**
**2016-02-25 17:10:47.040 multiThreadTest[3413:3631691] count=10,****当前线程:我的线程****2**
**2016-02-25 17:10:47.145 multiThreadTest[3413:3631692] count=11,****当前线程:**
**2016-02-25 17:10:47.249 multiThreadTest[3413:3631690] count=12,****当前线程:我的线程****1**
**2016-02-25 17:10:47.354 multiThreadTest[3413:3631691] count=13,****当前线程:我的线程****2**
**2016-02-25 17:10:47.458 multiThreadTest[3413:3631692] count=14,****当前线程:**
**2016-02-25 17:10:47.563 multiThreadTest[3413:3631690] count=15,****当前线程:我的线程****1**
**2016-02-25 17:10:47.668 multiThreadTest[3413:3631691] count=16,****当前线程:我的线程****2**
**2016-02-25 17:10:47.772 multiThreadTest[3413:3631692] count=17,****当前线程:**
**2016-02-25 17:10:47.877 multiThreadTest[3413:3631690] count=18,****当前线程:我的线程****1**
**2016-02-25 17:10:47.981 multiThreadTest[3413:3631691] count=19,****当前线程:我的线程****2**
**2016-02-25 17:10:48.085 multiThreadTest[3413:3631692] count=20,****当前线程:**
可以看到count的输出是有序的,如果去掉lock
**2016-02-25 17:11:47.786 multiThreadTest[3435:3632486] count=3,****当前线程:我的线程****2**
**2016-02-25 17:11:47.786 multiThreadTest[3435:3632487] count=1,****当前线程:**
**2016-02-25 17:11:47.786 multiThreadTest[3435:3632485] count=2,****当前线程:我的线程****1**
**2016-02-25 17:11:47.889 multiThreadTest[3435:3632486] count=4,****当前线程:我的线程****2**
**2016-02-25 17:11:47.889 multiThreadTest[3435:3632485] count=5,****当前线程:我的线程****1**
**2016-02-25 17:11:47.889 multiThreadTest[3435:3632487] count=6,****当前线程:**
**2016-02-25 17:11:47.994 multiThreadTest[3435:3632486] count=7,****当前线程:我的线程****2**
**2016-02-25 17:11:47.994 multiThreadTest[3435:3632487] count=9,****当前线程:**
**2016-02-25 17:11:47.994 multiThreadTest[3435:3632485] count=8,****当前线程:我的线程****1**
**2016-02-25 17:11:48.098 multiThreadTest[3435:3632486] count=11,****当前线程:我的线程****2**
**2016-02-25 17:11:48.098 multiThreadTest[3435:3632487] count=10,****当前线程:**
**2016-02-25 17:11:48.098 multiThreadTest[3435:3632485] count=12,****当前线程:我的线程****1**
**2016-02-25 17:11:48.203 multiThreadTest[3435:3632486] count=13,****当前线程:我的线程****2**
**2016-02-25 17:11:48.203 multiThreadTest[3435:3632485] count=14,****当前线程:我的线程****1**
**2016-02-25 17:11:48.203 multiThreadTest[3435:3632487] count=15,****当前线程:**
**2016-02-25 17:11:48.307 multiThreadTest[3435:3632486] count=16,****当前线程:我的线程****2**
**2016-02-25 17:11:48.308 multiThreadTest[3435:3632487] count=18,****当前线程:**
**2016-02-25 17:11:48.308 multiThreadTest[3435:3632485] count=17,****当前线程:我的线程****1**
**2016-02-25 17:11:48.412 multiThreadTest[3435:3632486] count=19,****当前线程:我的线程****2**
**2016-02-25 17:11:48.412 multiThreadTest[3435:3632485] count=20,****当前线程:我的线程****1**
**2016-02-25 17:11:48.412 multiThreadTest[3435:3632487] count=20,****当前线程:**
count出现了无序甚至重复的情况。lock的存在保证了lock之间的代码被原子操作。
2.NSCondition
我们用常见的生产者消费者问题来试验下
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"begin condition works!");
products = [[NSMutableArray alloc] init];
condition = [[NSCondition alloc] init];
[NSThread detachNewThreadSelector:@selector(createProducter) toTarget:self withObject:nil];
[NSThread detachNewThreadSelector:@selector(createConsumer) toTarget:self withObject:nil];
}
- (void)createConsumer
{
[condition lock];
while (products.count == 0) {
NSLog(@"wait for products");
[condition wait];
}
[products removeObjectAtIndex:0];
NSLog(@"comsume a product");
[condition unlock];
}
- (void)createProducter
{
[condition lock];
[products addObject:[[NSObject alloc] init]];
NSLog(@"produce a product");
[condition signal];
[condition unlock];
[condition lock];
while (products.count == 0) {
}
}
执行以上代码,得到的结果是:
**2016-02-25 17:14:54.199 multiThreadTest[3464:3634265] begin condition works!**
**2016-02-25 17:14:54.201 multiThreadTest[3464:3634329] produce a product**
**2016-02-25 17:14:54.201 multiThreadTest[3464:3634330] comsume a product**
condition 的 wait 使当前的操作进入等待状态,而signal则使其继续往下执行