First we need to know that the CPU can only handle a thread at the same time, multi-threaded concurrency is not multiple threads at the same time, but the CPU is constantly switching between threads, so the thread is not the more the better, when there are a large number of threads, will let the CPU in the switch between the busy, rather than development.
Specifically, the iOS aspect of multithreading is also two, pthread and Nsthread. Pthread is a C language write multi-threading, the advantage is not only used in iOS mobile development, basically support C language can be used, the disadvantage is C language common, difficult to identify, hard to remember and does not support arc. So in iOS multithreaded development or to understand nsthread better, Pthread master can.
In the use of OC objects, the most common is alloc and init, object memory allocation and instantiation. So it's possible that this is happening:
Nsthread *thread1 = [[Nsthread alloc] init];
[Thread1 start];
In fact, this is not possible, nsthread cannot be instantiated directly, but you can derive subclasses to instantiate it. To use a thread object, you can do this in this way:
Nsthread *thread2 = [[Nsthread alloc] initwithtarget:self selector: @selector (runthread) Object:nil];
[Thread2 start];
Of course, if you do not want to use the object mode to open threads, there is a simpler way:
[Nsthread detachnewthreadselector: @selector (runthread) totarget:self Withobject:nil];
Of course, this way there is no way to use some of the thread's properties. You can use Thread2.name to customize a name for a thread so that when a thread crashes, you can directly navigate to the left-hand crash list to which thread is having the problem. You can also set the priority of a thread by threadpriority, but setting the priority only increases the probability that the thread will be called, rather than calling the high-priority thread first.
As you can see, although Thread2 is called first, it is still interspersed with thread3 calls. (in multi-threaded development, do not believe in one or a few times the result of running);
There are five thread states, Nsthread can help us to manage four of them, one is to create a thread pool, the second is [thread start], this is not to open the thread directly, but to let the thread into the preparation state, Runloop can be called at any time, The third is runing, this is beyond our control, the fourth is the sleep state or call blocking state, when the thread runs out or we call sleep to do this state, the fifth is dead, you can kill the thread through exit.
Further learning, what do we use threads for? Of course the time-consuming operation is inconvenient to put on the main thread type call. After we have processed the data, or to modify the UI, is it possible to modify it directly? Of course not, Iosui use is in the main thread, all have the use of inter-thread communication technology, to ensure a secure and accurate update of the UI
-(void) Performselectoronmainthread: (SEL) Aselector Withobject: (nullable ID) arg waituntildone: (BOOL) wait;
The first sel is the method that executes on the main thread, the second arg is the parameter that needs to be passed in, the third wait indicates whether the current thread waits, and if yes, the thread will continue until the SEL method is executed.
In a child thread, you might be able to update the UI successfully, but there are a lot of uncertainties, such as running OK today, and running again tomorrow will hang up, leaving you completely puzzled.
By referring to the above method, one must be aware of another
[Self performselector: @selector (othrerunthread) onthread:thread2 Withobject:nil Waituntildone:no];
You will find that when you submit the function into the thread2, the function is not executed. This involves the Runloop,runloop will open a dead loop, so that the execution of the thread to form a circle, when the execution will be executed again from the beginning, and constantly loop to ask the system whether there are methods to execute, we open the sub-thread is not joined in the loop, like a line, it has come to the bottom, You add the function from the beginning of the line, and the thread does not go back to execution. We can use the implementation runloop in the thread
-(void) runthread{
[[Nsrunloop Currentrunloop] run];
NSLog (@ "------%@,%@-------", Nsstringfromselector (_cmd), [Nsthread CurrentThread]);
}
However, this is not a good choice, so it opens up a dead loop thread that always consumes resources, and we don't have a good way to stop this loop.
-(void) runthread{
while (!self.isfinshed) {
[[Nsrunloop Currentrunloop] Runmode:nsdefaultrunloopmode beforedate:[nsdate datewithtimeintervalsincenow:0.1f]];
}
NSLog (@ "------%@,%@-------", Nsstringfromselector (_cmd), [Nsthread CurrentThread]);
}
-(void) othrerunthread{
self.finshed = YES;
NSLog (@ "------%@,%@-------", Nsstringfromselector (_cmd), [Nsthread CurrentThread]);
}
We can use a global judgment condition to set, when the Othrerunthread is not executed, we turn on the loop for 0.1 seconds, constantly let the thread to ask whether there is a method is not executed, when the method has been executed, no longer loop.
iOS Learning-multithreaded development (Nsthread)