Default is Zero.

//——————————————————————NSThread多线程的开启————————————————————————————————

// 1.第一种开启新的线程调用

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

[thread start]; //需要手动开启

// 2.第二种开启新的线程调用 mutableThread

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

// 3.第三种开启新的线程调用 mutableThread [self performSelectorInBackground:@selector(mutableThread) withObject:nil];

//——————————————————————线程队列NSOperationQueue——————————————————————————

//1.block语法启动一个线程

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

[threadQueue1 addOperationWithBlock:^{ NSThread *t=[NSThread currentThread]; if (![t isMainThread]) { NSLog(@"是主线程"); } }];

//2.NSOperationQueue 队列 NSOperation开启一个线程

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

//操作任务

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

[threadQueue2 addOperation:op];

//在主线程上调用 reloadData 方法

[self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

//3.队列的执行顺序(FIFO 先进先出)

//创建队列

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

// //暂停队列//[queue setSuspended:YES];

//queue队列执行操作的个数 默认-1(设置会限制queue队列能执行多少个操作) queue.maxConcurrentOperationCount=NSOperationQueueDefaultMaxConcurrentOperationCount;

//创建操作

NSBlockOperation *op1=[NSBlockOperation blockOperationWithBlock:^{ NSLog(@"task op1"); }];

NSBlockOperation *op2=[NSBlockOperation blockOperationWithBlock:^{ NSLog(@"task op2"); }];

NSBlockOperation *op3=[NSBlockOperation blockOperationWithBlock:^{ NSLog(@"task op3"); }]; /

/添加到队列中

[queue addOperation:op1];

[queue addOperation:op2];

[queue addOperation:op3];

//添加依赖 op2执行完之后才执行op1

[op1 addDependency:op2];

//优先级 /* NSOperationQueuePriorityVeryLow = -8L, NSOperationQueuePriorityLow = -4L, NSOperationQueuePriorityNormal = 0, NSOperationQueuePriorityHigh = 4, NSOperationQueuePriorityVeryHigh = 8 */

//设置执行优先级

op3.queuePriority=NSOperationQueuePriorityVeryHigh;

//(1)添加到队列的顺序

//(2)操作执行的优先级

//(3)操作的依赖关系

//—————————————————————GCD(Grand Central DisPatch)————————————————————————

/* 队列执行优先级 #define DISPATCH_QUEUE_PRIORITY_HIGH 2 #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 ———>全局队列 #define DISPATCH_QUEUE_PRIORITY_LOW (-2) #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN */

//全局队列(可能会开启多条线程)

dispatch_queue_t global=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//串行队列(只可能会开启一条线程)

//#define DISPATCH_QUEUE_SERIAL NULL

dispatch_queue_t queue2=dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);

//主队列

dispatch_queue_t main=dispatch_get_main_queue();

//GCD使用

//1.创建Serial Dispatch Queue(串行队列)

dispatch_queue_t q1=dispatch_queue_create("com.wxhl.serial1", NULL);

dispatch_queue_t q2=dispatch_queue_create("com.wxhl.serial2", DISPATCH_QUEUE_SERIAL);

//往队列q1添加操作 dispatch_async(q1, ^{ ; });

//往队列q2添加操作 dispatch_async(q2, ^{ ; });

//2.创建Concurrent Dispatch Queue(并行队列)

dispatch_queue_t q3=dispatch_queue_create("com.wxhl.concurrent", DISPATCH_QUEUE_CONCURRENT); /

/往队列q3添加操作 dispatch_async(q3, ^{ ; });

// dispatch_release;

// dispatch_retain;

#warning mark- ios6之后,GCD兼容了ARC 不需要手动管理GCD内存了

//——————————————————同步/异步操作,死锁问题——————————————————————————————————

/* 所谓死锁: 是指两个或两个以上的进程在执行过程中,,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。 */

/*操作步骤: 1.拿到主队列 2.分配同步/异步操作 */

dispatch_queue_t mainT=dispatch_get_main_queue();

//异步操作:

dispatch_async dispatch_async(mainT, ^{ //做什么操作 dispatch_sync(mainT, ^{ //做什么操作 NSLog(@"sync间接引发了死锁"); }); });

//同步操作:

dispatch_sync —->容易引起死锁 使用要注意 dispatch_sync(mainT, ^{ //做什么操作 NSLog(@"sync直接引发了死锁"); });

乐观者在灾祸中看到机会;悲观者在机会中看到灾祸

Default is Zero.

相关文章:

你感兴趣的文章:

标签云: