Main queue synchronization
Main queue synchronization does not start threads */
- (void)mainSyncTest{
NSLog(@"0");
/ / a deadlock
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"1");
NSLog(@"% @",[NSThread currentThread]);
});
NSLog(@"2");
}
Copy the code
Run print result
Main queue asynchronous
Main queue asynchrony does not have thread order */
- (void)mainAsyncTest{
NSLog(@"0");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"1");
NSLog(@"% @",[NSThread currentThread]);
});
NSLog(@"2");
}
Copy the code
Run print result
Global queue synchronization
Global queue: a concurrent queue is synchronized on the main thread */
- (void)globalSyncTest{
for (int i = 0; i<10; i++) {
dispatch_sync(dispatch_get_global_queue(0.0), ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"hello queue");
}
Copy the code
Run print result
4. Global column asynchrony
/** Global asynchronous global queue: a concurrent queue */
- (void)globalAsyncTest{
for (int i = 0; i<10; i++) {
dispatch_async(dispatch_get_global_queue(0.0), ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"hello queue");
}
Copy the code
Run print result
Custom serial queue synchronization
/** Serial synchronization queue: FIFO: first in first out (executed in the current thread) */
- (void)serialSyncTest{
//1: create a serial queue
NSLog(@"hello");
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i<10; i++) {
dispatch_sync(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
Copy the code
Run print result
Custom serial queue asynchrony
/** Serial asynchronous queue (start one thread task after another) */
- (void)serialAsyncTest{
//1: create a serial queue
NSLog(@"hello");
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i<100; i++) {
dispatch_async(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
Copy the code
Run print result
Custom concurrent queue synchronization
/** Synchronous concurrency: block synchronous lock queue: Resume supend thread operation, queue pending task can be executed (will not open the thread to execute in the current thread) */
- (void)concurrentSyncTest{
NSLog(@"hello");
//1: creates a concurrent queue
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i<10; i++) {
dispatch_sync(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
Copy the code
Run print result
Custom concurrent queue asynchrony
/** Asynchronous concurrency: having asynchronous functions does not necessarily open up threads (start threads to execute in the current thread) */
- (void)concurrentAsyncTest{
NSLog(@"hello");
//1: creates a concurrent queue
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i<100; i++) {
dispatch_async(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
Copy the code
Run print result
9. Concurrent queue – asynchronous – synchronous
- (void)concurrentASync_Sync{
// Synchronize the queue
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// Asynchronous function
dispatch_async(queue, ^{
NSLog(@"2");
/ / synchronize
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
Concurrent queue-asynchronous – asynchronous
- (void)concurrentASync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
/ / time consuming
dispatch_async(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
11. Concurrent queue – sync – sync
- (void)concurrentSync_Sync{
// Synchronize the queue
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// Asynchronous function
dispatch_sync(queue, ^{
NSLog(@"2");
/ / synchronize
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
12. Concurrent queue – synchronous – asynchronous
- (void)concurrentSync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
/ / time consuming
dispatch_sync(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
Serial queue-asynchronous – synchronous
- (void)serialAsync_Sync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_async(queue, ^{
NSLog(@"2");
/ / a deadlock
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
Serial queue-asynchronous – asynchronous
- (void)serialAsync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_async(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
Xv. Serial queue – synchronous – asynchronous
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_sync(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
Copy the code
Run result printing
Serial queue-sync – sync
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_sync(queue, ^{
NSLog(@"2");
/ / a deadlock
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5"); }}Copy the code
Run result printing
Xvii. Concurrency — dispatch_barrier_sync
- (void)concurrent_barrierSync
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (NSInteger i = 0; i < 10; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
dispatch_barrier_sync(concurrentQueue, ^{
NSLog(@"barrier");
});
for (NSInteger i = 10; i < 20; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i); }); }}Copy the code
Run print result
Xviii. Concurrency — dispatch_barrier_async
- (void)concurrent_barrierASync
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (NSInteger i = 0; i < 10; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"barrier");
});
for (NSInteger i = 10; i < 20; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i); }); }}Copy the code
Run print result
Xvii. Individual cases
- (void)perform {
dispatch_async(dispatch_get_global_queue(0.0), ^{
[self performSelector:@selector(test) withObject:nil afterDelay:0]; }); } - (void)test{
NSLog(@"hello word");
}
Copy the code
The hello Word method is not printed to create the submit task on the runloop, and the GCD layer creates a thread that does not have runloop enabled by default, so this method will fail. If you change dispatch_GEt_global_queue to the main queue, since the main queue has runloop enabled by default, it will execute (change dispatch_async to dispatch_sync, because synchronization is performed on the current thread, If the current thread is the main thread, then the test method will execute.