Small valley bottom exploration collection
- In the study
GCD
First, here’s oneFunctions and queues
So that,GCD
It’s easier to understand
Article 1. The concept
1.1. Functions in GCD
-
- Task using
block
encapsulation
- Task using
-
- The task of
block
There are no arguments and no return values
- The task of
-
- A function that performs the task
- 3.1. The asynchronous
dispatch_async
The next statement can be executed without waiting for the current statement to complete
Opens the thread to execute the block’s task
Asynchrony is a byword for multithreading
- 3.2. The synchronous
dispatch_sync
You must wait for the current statement to complete before executing the next statement
Thread will not be opened
The current task of the block is executed
1.2. The queue
-
- Here’s a graphic picture of the queue:
-
Serial queues
Analysis: is to queue, just one channel:FIFO (First in, first out)
-
Parallel lines
Analysis: Suppose you buy food at a window. There are four Windows
The feeling is changed to eat, better understand ~~
1.3. Combination of functions and queues
-
Synchronous function serial queue
- Does not start the thread and executes the task in the current thread
- Tasks are executed sequentially, one task after another
- You can get clogged up
-
Synchronous function concurrent queue
- Does not start the thread and executes the task in the current thread
- One task after another
-
Asynchronous function serial queue
- Start a new thread
- One task after another
-
Asynchronous function concurrent queue
- Start the thread and execute the task in the current thread
- Tasks are executed asynchronously, in no order, and
CPU
Scheduling the
Application 2.
- The application of functions and queues is explained by interview questions.
2.1. Interview Question 1
- Write the following program output
- (void)textDemo1{
dispatch_queue_t queue = dispatch_queue_create("question1", DISPATCH_QUEUE_CONCURRENT);
NSLog(@ "1");
dispatch_async(queue, ^{
NSLog(@ "2");
dispatch_sync(queue, ^{
NSLog(@ "3");
});
NSLog(@ "4");
});
NSLog(5 "@");
}
Copy the code
Analysis:
- This is a parallel queue
- Both synchronous and asynchronous are time consuming
- So the first one out is the main line
From 1 to 5
- In parallel queues are synchronous queues, so they are executed sequentially
Answer: the 1-5-2-3-4
2.2. Interview Question 2
- Write the following program output
- (void)textDemo2{
// Synchronize the queue
dispatch_queue_t queue = dispatch_queue_create("question2".NULL);
NSLog(@ "1");
// Asynchronous functions
dispatch_async(queue, ^{
NSLog(@ "2");
/ / synchronize
dispatch_sync(queue, ^{
NSLog(@ "3");
});
NSLog(@ "4");
});
NSLog(5 "@");
}
Copy the code
Analysis of the
- This is a serial queue:
#define DISPATCH_QUEUE_SERIAL NULL
- The first one out is the main line
From 1 to 5
- Then the output
2
.- After that, people got a little confused. I drew a picture:
Piece of task
addThree tasks
And thenThree tasks
Waiting for theFour tasks
Performed,Four tasks
Waiting for thePiece of task
Performed,Piece of task
Waiting for theThree tasks
after- And that’s what happens
A deadlock
Answer: 1-5-2- stuck
2.3. Interview question 3
-
Multiple choice: The following program output might be ()
A : 1230789
B : 1237890
C : 3120798
D : 2137890
- (void)textDemo3{
dispatch_queue_t queue = dispatch_queue_create("question3", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@ "1");
});
dispatch_async(queue, ^{
NSLog(@ "2");
});
dispatch_sync(queue, ^{
NSLog(@ "3");
});
NSLog(@ "0");
dispatch_async(queue, ^{
NSLog(@ "7");
});
dispatch_async(queue, ^{
NSLog(8 "@");
});
dispatch_async(queue, ^{
NSLog(@ "9");
});
}
Copy the code
Analysis:
- This is a
Parallel lines
.Asynchronous thread 1, 2
和Synchronous thread 3
It is not certain who finishes firstThree tasks
isThread synchronization
, so,3
Must be in0
In front of theThe 7-8-9 mission
isAsynchronous thread
So it’s not in order, but it’s definitely there0
after
Answer: AC