//进程:一个正在运行的可执行文件,每个进程都有独立的虚拟内存空间和系统资源,包括端口权限等,并且至少包括一个主线程和任意数量的辅助线程,另外,当一个进程的主线程退出时,这个进程就结束了
//线程:一个独立的代码执行路径(线程是代码执行的最小分支),由线程做资源的分配和调度
//什么时候需要创建线程?
//当我们在程序中遇见所写的程序是非常大,非常繁琐,而且会影响到用户体验的时候我们需要用到多线程
//主线程和子线程的去区别:
//1.能够分配资源的最大容量不同,主线程10MB,子线程5MB
//2.子线程不会释放autorelease的对象,需要手动添加自动释放池
//3.更新UI的操作的必须在主线程中执行
//在iOS中创建多线程的方式有多种下面将为大家一一介绍:
1.NSObject,此方法执行的操作在后台线程中执行
[self performSelectorInBackground:@selector(serverPro:) withObject:@"周新池"];
- (void)serverPro:(id)string {
for (NSInteger i = 0; i <= 10; i++) {
[NSThread sleepForTimeInterval:1];
NSString *string = [NSString stringWithFormat:@"欢迎光临,我是第%ld号,很高兴为你服务", i];
NSLog(@"%@", string);
}
}
2.NSThread,线程类,继承于NSObject
// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(serverPro:) object:@"李晓强"];
//线程命名
// thread.name = @"辅助线程";
// NSLog(@"%@", thread);
// //执行线程
// [thread start];
// [thread release];
//NSThread的一些属性
//获取主线程
// NSThread *mainThread = [NSThread mainThread];
// NSLog(@"%@", mainThread);
//获取当前代码执行的线程
// NSThread *currentThread = [NSThread currentThread];
// NSLog(@"获取当前线程:%@", currentThread);
//线程休眠
// [NSThread sleepForTimeInterval:2];
//方法
- (void)serverPro:(id)string {
for (NSInteger i = 0; i <= 10; i++) {
[NSThread sleepForTimeInterval:1];
NSString *string = [NSString stringWithFormat:@"欢迎光临,我是第%ld号,很高兴为你服务", i];
NSLog(@"%@", string);
}
}
//3.NSOperationQueue,操作队列(任务队列),继承于NSObject,操作队列绑定得有一个线程
//任务(操作):一段代码
//NSOperation,任务(操作)类,继承于NSObject ,是一个抽象基类
//其子类NSInvocationOperation,NSBlockOperation
//串行, 并发,主要的区别在于同时执行的任务的数量
//串行:一次只能执行一个任务,并且必须等上一个任务执行完才能执行下一个任务
//并发:允许多个任务同时执行
//创建任务
//任务只能够执行一次,已经添加到队列中就不能在添加了
NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(serverPro:) object:nil];
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
for (NSInteger i = 1; i <= 100; i++) {
NSLog(@"%ld", i);
}
}];
//执行任务
// [invocation start];
//任务依赖关系
[invocation addDependency:block];
//创建操作队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//最大并发数,默认是-1,代表无限,代表并发, 1为串行
queue.maxConcurrentOperationCount = 1;
//把任务放到队列中,一旦添加到队列,任务就会在队列绑定的线程中执行
[queue addOperation:invocation];
[queue addOperation:block];
//快捷添加到队列1
[queue addOperationWithBlock:^{
NSLog(@"添加到队列");
}];
//方式2
// [queue addOperations:@[invocation, block] waitUntilFinished:NO];
//取消操作
//一但任务添加到队列中,不能移除,只能取消
//并且只能取消没有执行的操作
//取消单个任务
[block cancel];
//取消所有任务
[queue cancelAllOperations];
//释放
[queue release];
*/
//4.GCD:Grand Central Dispatch, 大中央调度,基于CPU的多核技术实现,使用C语言的语法,执行效率更高,NSOperationQueue基于GCD实现
//三种队列
//主队列,绑定主线程,串行队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
NSLog(@"%@", mainQueue);
//全局队列,绑定的子线程,并发队列
//参数1:队列优先级
//DISPATCH_QUEUE_PRIORITY_HIGH 高
//DISPATCH_QUEUE_PRIORITY_DEFAULT 默认
//DISPATCH_QUEUE_PRIORITY_LOW 低
//DISPATCH_QUEUE_PRIORITY_BACKGROUND 后台
//参数2:预留参数,为未来做准备
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"%@", globalQueue);
//自定义队列,绑定是子线程,串行或并发
//参数1:队列标识符,域名
//参数2:并发DISPATCH_QUEUE_CONCURRENT 或串行DISPATCH_QUEUE_SERIAL
dispatch_queue_t customQueue = dispatch_queue_create("com.lanou3g.heihei", DISPATCH_QUEUE_SERIAL);
NSLog(@"%@", customQueue);
//把操作放到队列方式:
//同步执行
// dispatch_sync(mainQueue, ^{
// //操作
// [self serverPro:nil];
// });
//异步执行
// dispatch_async(globalQueue, ^{
// [self serverPro:nil];
// });
//多少秒后执行某个操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"睡觉");
});
//程序运行期间只执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Hello,world");
});
//高级接客
- (void)serverPro:(id)string {
//自己手动加的自动释放池
@autoreleasepool {
//获取当前代码执行的线程
// NSThread *currentThread = [NSThread currentThread];
// NSLog(@"获取当前线程:%@", currentThread);
// NSLog(@"%@", string);
for (NSInteger i = 0; i <= 10; i++) {
//线程休眠
[NSThread sleepForTimeInterval:1];
NSString *string = [NSString stringWithFormat:@"欢迎光临,我是第%ld号,很高兴为你服务", i];
NSLog(@"%@", string);
}
//跳回到主线程执行某个方法
[self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
}
}
//**********************************************下面给大家做一个火车票售票程序*************************************************
@interface TicketViewController () {
NSInteger count;//总票数
NSInteger index;//第几张票
NSLock *lock;//锁
}
@end
@implementation TicketViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
count = 100;
index = 1;
self.view.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.170];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 100, 335, 100);
// button.backgroundColor = [UIColor blackColor];
button.titleLabel.font = [UIFont boldSystemFontOfSize:30];
[button setTitle:@"开始卖票" forState:UIControlStateNormal];
[button addTarget:self action:@selector(start) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
//NSLock,锁类, 继承于NSObject,为了防止多个线程抢夺资源,经常会为你这个资源加上锁
lock = [[NSLock alloc] init];
// //上锁
// [lock lock];
// //解锁
// [lock unlock];
//多线程可以加速程序的执行,把一些复杂的操作放在子线程中执行,避免阻塞主线程
//当多个线程共同操作同一块资源时,需要对资源进行管理(比如上锁),避免出现抢夺资源
}
- (void)start {
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(sale) object:nil];
thread.name = @"窗口1";
[thread start];
[thread release];
NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sale) object:nil];
thread1.name = @"窗口2";
[thread1 start];
[thread1 release];
}
- (void)sale {
// if (index <= 0) {
// return;
// }
// for (NSInteger i = 1; i <= count; i++) {
// [NSThread sleepForTimeInterval:1];
// NSLog(@"%@,卖了%ld张票,剩余%ld张票",[NSThread currentThread].name, i, --index);
// }
while (YES) {
[NSThread sleepForTimeInterval:1];
[lock lock];
if (count > 0) {
//有票
count--;
NSLog(@"%@,卖掉了%ld号票,剩余%ld张票",[NSThread currentThread].name, index, count);
index++;
}else{
//没票
NSLog(@"票卖完了");
break;
}
[lock unlock];
}
}
多线程其他的知识点大家可以在IPA文档中自己查看并书写,祝大家编程苦逼.谢谢