天天看點

OC中多線程的建立方法

方法一:

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

方法二:

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

方法三:

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

方法四:多線程blog建立

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

//會開啟一個多線程

[operationQueue addOperationWithBlock:^{

for(int i = 0; i < 50 ;i++)

{

NSLog(@"多線程:%d",i);

}

}];

方法五:

//相當于是一個線程池

operationQueue.maxConcurrentOperationCount = 1;//設定并發數

//建立線程

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

//設定線程的優先級

[opertion1 setQueuePriority:NSOperationQueuePriorityVeryLow];

//建立另一個線程

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

[opertion2 setQueuePriority:NSOperationQueuePriorityHigh];

方法六:

dispatch_queue_t queue = dispatch_queue_create("test",NULL);

dispatch_async(queue,^{

for(int i=0;i<50;i++)

NSLog(@"多線程:%d",i);

});