您的位置:首页 > 其它

多线程

2015-12-17 20:18 399 查看
- (void)viewDidLoad {

[super viewDidLoad];

// 进程和线程的区别

//程序:有源代码生成的可执行的应用

// 进程:一个正在运行的程序可以看做一个进程,进城拥有对立运行所需的全部资源

//线程:程序中独立运行的代码段,如接受QQ消息的代码

//

self.view.backgroundColor = [UIColor redColor];

// 多线程

//当程序运行的时候会默认为主线程分配1M的栈空间,会为子线程默认分配512K的栈空间,分配的栈空间必须是4K的整数倍.(栈内存值储存变量,所以512K足够用).主线程和子线程地内存是不共用的

//子线程正在运行期间,会在堆区存放对象,由于子线程是自行开辟的内存空间,和主线程不同(主线程从maim函数开始,默认在autoreleasepool中),如果短时间内开辟大量的内存空间,会导致内存占有率机急剧上升,从而使内催产生crash.所以我们子线程的方法,要手动书写自动释放池.主线程和子线程对内存是公用的;

//脱离线程: 线程内操作执行完毕以后,线程生命救赎,被销毁;

//非脱离线程:线程内操作执行完毕后,线程不会被销毁,等待再次欢唤醒后,继续执行操作

//1.NSObject自带的多线程

// [self performSelectorInBackground:@selector(calculateNumbers) withObject:nil];

//2.NSThread

//

// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculateNumbers) object:nil];

// //需要手动开启线程

// [thread start];

//取消

// [thread cancel];

//方式二:

//不需要手动开启

[NSThread detachNewThreadSelector:@selector(calculateNumbers) toTarget:self withObject:nil];

// 3.NSOperation/NSOperationQueue

NSInvocationOperation *invocation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(calculateNumbers) object:nil];

NSInvocationOperation *invocation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(calculateNumbers) object:nil];

NSBlockOperation *block1 = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"+++%@--%d",[NSThread currentThread],[NSThread isMainThread]);

}];

NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"+++%@--%d",[NSThread currentThread],[NSThread isMainThread]);

}];

//队列

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

//设置最大并发数(可以同时执行任务的最大操作个数)

[queue setMaxConcurrentOperationCount:3];

[queue addOperation:invocation1];

[queue addOperation:block1];

//4.GCD

}

-(void)calculateNumbers {

@autoreleasepool {

//打印当前线程(number只要不是1就是子线程)

NSLog(@"=%@",[NSThread currentThread]);

//打印是否是主线程

NSLog(@"=%d",[NSThread isMainThread]);

int sum = 0;

for (int i= 0; i< 635500000; i ++) {

sum +=1;

}

}

}

#import "ViewController.h"

void function(void * string);

void function(void * string) {

printf("%s\n",string);

}

@interface ViewController ()

@property(strong,nonatomic)NSLock *lock;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.lock = [NSLock new];

//GCD

//主队列:是串行队列 (单例)

//全局队列:并行队列 (单例)

//自定义队列:自定义串行或者并行

//串行队列:(遵循FIFO)所有操作顺序执行

//并行队列:(遵循FIFO)所有操作并发执行

//除了主队列以外,无论是穿行队列还是并行队列,都是子线程.

//无论是主队列还是全局队列,组定义队列,再添加task的时候都可以使用同样的方式

// 返回主线程

// 1. dispatch_get_main_queue()

// 2. - (void)performSelectorOnMainThread:(SEL)aSelector

//withObject:(id)arg waitUntilDone:(BOOL)wait

// //获取主队列

// dispatch_queue_t mainQueue = dispatch_get_main_queue();

// //给队列添加操作

// dispatch_async(mainQueue, ^{

// NSLog(@"1--%d",[NSThread isMainThread]);

// });

// dispatch_async(mainQueue, ^{

// NSLog(@"2--%d",[NSThread isMainThread]);

// });

// dispatch_async(mainQueue, ^{

// NSLog(@"3--%d",[NSThread isMainThread]);

// });

//

//获取全局队列

dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//添加同步任务(block不执行完,后面代码不会执行),需要写在提前执行的位置

dispatch_sync(globalQueue, ^{

NSLog(@"等我");

});

dispatch_async(globalQueue, ^{

NSLog(@"1--%d",[NSThread isMainThread]);

});

dispatch_async(globalQueue, ^{

NSLog(@"2--%d",[NSThread isMainThread]);

});

dispatch_async(globalQueue, ^{

NSLog(@"3--%d",[NSThread isMainThread]);

});

//添加函数

dispatch_async_f(globalQueue, "李聪", function);

//// 添加一个任务(task),延迟10秒后执行

// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), globalQueue, ^{

// NSLog(@"出来吧,神龙");

// });

// //向队列中添加一个任务,反复执行

// //t:添加进去的顺序

// dispatch_apply(3, globalQueue, ^(size_t t) {

//

// NSLog(@"重要的事情说三遍--你猜");

// });

//

// //创建一个分组

// dispatch_group_t group = dispatch_group_create();

// //将任务添加到分组中

// dispatch_group_notify(group, globalQueue, ^{

//

// NSLog(@"青龙学习小组专用1");

// });

//

// dispatch_group_notify(group, globalQueue, ^{

//

// NSLog(@"青龙学习小组专用2");

// });

//

// dispatch_group_notify(group, globalQueue, ^{

//

// NSLog(@"青龙学习小组专用3");

// });

//创建自定义队列

//串行队列(自定义队列都是子线程)

// dispatch_queue_t serialQueue = dispatch_queue_create("我的串行队列标签", DISPATCH_QUEUE_SERIAL);

//获取队列标签

// const char * label = dispatch_queue_get_label(serialQueue);

// NSLog(@"%@",[NSString stringWithUTF8String:(const char *)label]);

// //并行队列

dispatch_queue_t concurrentQueue = dispatch_queue_create("并行队列标签", DISPATCH_QUEUE_SERIAL);

dispatch_async(concurrentQueue, ^{

for (int i = 0; i <1000; i ++) {

[self buyTicket];

}

});

dispatch_async(concurrentQueue, ^{

for (int i = 0; i <1000; i ++) {

[self buyTicket];

}

});

}

int count = 5000;

-(void)buyTicket {

//加锁

[self.lock lock];

count --;

NSLog(@"余票数为%d",count);

//解锁

[self.lock unlock];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: