multithreading
There are three main types: NSThread, NSoperationQueue, and GCD
NSThread
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(testThread: object:@" I am an argument "); [thread start]; Thread. name = @"NSThread thread "; // Adjust the permission of Thread The permission range of Thread is 0 to 1. Thread.threadpriority = 1; threadPriority = 1; threadPriority = 1; threadPriority = 1; // Cancel the currently started thread [thread cancel]; [NSThread detachNewThreadSelector:@selector(testThread:) toTarget:self withObject:@" constructor method "];Copy the code
GCD
Cancel the task
- (void)gcdBlockCancel{
dispatch_queue_t queue = dispatch_queue_create("com.gcdtest.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_block_t block1 = dispatch_block_create(0, ^{
sleep(3);
NSLog(@"block1 %@",[NSThread currentThread]);
});
dispatch_block_t block2 = dispatch_block_create(0, ^{
NSLog(@"block2 %@",[NSThread currentThread]);
});
dispatch_block_t block3 = dispatch_block_create(0, ^{
NSLog(@"block3 %@",[NSThread currentThread]);
});
dispatch_async(queue, block1);
dispatch_async(queue, block2);
dispatch_async(queue, block3);
dispatch_block_cancel(block3);
}
Copy the code
Control the maximum concurrency
-
A semaphore is an integer value with an initial count; Can receive notification signals and wait signals. When a semaphore receives a notification signal, the count is +1; When the semaphore receives the waiting signal, the count is -1; If the semaphore is zero, the thread blocks until the semaphore is greater than zero before continuing.
-
The semaphore mechanism allows synchronization of threads and also controls the maximum number of concurrent requests. Here is the code to control the maximum number of concurrent requests
dispatch_queue_t concurrentQueue =dispatch_queue_create("plz", DISPATCH_QUEUE_CONCURRENT); dispatch_queue_t seriaQueue = dispatch_queue_create("s", DISPATCH_QUEUE_SERIAL); dispatch_semaphore_t semaphore =dispatch_semaphore_create(4); for (int i =0; i<10; i++) { dispatch_async(seriaQueue, ^{ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); Dispatch_async (concurrentQueue, ^{NSLog(@"thread-info:%@ start task %d",[NSThread currentThread],(int) I); sleep(1); NSLog(@"thread-info:%@ End task %d",[NSThread currentThread],(int) I); dispatch_semaphore_signal(semaphore); }); }); } NSLog(@" main thread... !" );Copy the code
NSOperationQueue
Control the maximum concurrency
NSOperationQueue, has considered the maximum number of concurrent problems, and provides maxConcurrentOperationCount attribute set maximum concurrency (the attribute need to be done before the task is added to the queue Settings). MaxConcurrentOperationCount the default value is 1; If the value is set to 0, no task is performed; If the value is set to 1, the queue is serial; If it’s greater than 1, it’s parallel.
NSOperationQueue *queue = [[NSOperationQueue alloc]init]; queue.maxConcurrentOperationCount = 3; for (int i =0; i<10; I ++) {[queue addOperationWithBlock:^{NSLog(@"thread-info:%@ Start task %d",[NSThread currentThread],(int) I); sleep(1); NSLog(@"thread-info:%@ End task %d",[NSThread currentThread],(int) I);}]; }Copy the code
Back to the main thread
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"***%@", [NSThread currentThread]);
}];
Copy the code
Cancel the task
- (void)addCustomeOperationQueue {NSLog(@" Create add task %@",[NSThread currentThread]); NSOperationQueue *customQueue = [[NSOperationQueue alloc]init]; customQueue.maxConcurrentOperationCount = 5; NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task:) object:@"1"]; [customQueue addOperation:op1]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task:) object:@"2"]; NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task:) object:@"3"]; NSInvocationOperation *op4 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task:) object:@"4"]; // This method can only cancel operations that have not yet been executed. If the operation has already been executed, the method still cannot be cancelled. [customQueue cancelAllOperations]; To cancel an operation, you can call the cancellation method of the operation directly. [op1 cancel]; [customQueue addOperation:op2]; [customQueue addOperation:op3]; [customQueue addOperation:op4]; }Copy the code
To be updated…