多線程與GCD

//——————————————————————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直接引發了死鎖"); });

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章