// NSThread 创建线程
// 1. initWithTarget:selector:object: 创建,需要调用start方法进行启动
NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
[thread1 start];
// 2.
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// gcd 创建线程
// 1. 创建串行队列;将任务提交给队列,必须等第一个任务完成后,才执行第二个任务
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^(void)
{
for (int i=0; i<10; i++) {
NSLog(@"serialQueue .... %d", i);
}
});
dispatch_async(serialQueue, ^(void)
{
for(int i=10; i<20; i++)
{
NSLog(@"serialQueue ... %d", i);
}
});
// 2. 创建并发队列;将任务提交给队列,第一个任务和第二个任务,并行执行
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^(void)
{
for(int i=0; i<10; i++)
{
NSLog(@"concurrentQueue...i=%d", i);
}
});
dispatch_async(concurrentQueue, ^(void)
{
for(int i=10;i<20;i++)
{
NSLog(@"concurrentqueue...i=%d",i);
}
});
// 全局并发队列
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void)
{
for(int i=0; i<10; i++)
{
NSLog(@"global_queue...i=%d", i);
}
});
// 主线程
dispatch_async(dispatch_get_main_queue(), ^(void)
{
NSLog(@"main thread...");
});