iOS multithreaded development Brief analysis of NSThread

  • 2020-06-03 08:29:01
  • OfStack

In iOS development, there are three main ways to achieve multithreading, NSThread, NSOperation and GCD. In my previous blog, NSOperation and GCD have been implemented in detail. In order to learn the integrity, today we mainly implement NSThread from the code level. Case code uploaded to https: / / github com/chenyufeng1991 / NSThread.

(1) Initialize and start a thread


  - (void)viewWillAppear:(BOOL)animated
  {
  [super viewWillAppear:animated];
  // Get the current thread 
  NSThread *current = [NSThread currentThread];
  NSLog(@" Current thread is  %@",current);
  // Initializing thread 
  NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
  // Set the priority of the thread (0.0-1.0)
  thread.threadPriority = 1.0;
  thread.name = @" A new thread 1";
  [thread start];
  }
  - (void)run
  {
  NSLog(@" Threads execute ");
  // Get the current thread 
  NSThread *current = [NSThread currentThread];
  NSLog(@" Current thread is  %@",current);
  // Thread sleep to simulate time-consuming operations 
  [NSThread sleepForTimeInterval:2];
  // Get the main thread 
  NSThread *mainThread = [NSThread mainThread];
  NSLog(@" Get the main thread in the child thread  %@",mainThread);
  }

currentThread, a useful method, is often used to determine in which thread a method is executed.

(2)NSThread can specify a thread to execute in the background:



  // The background to create 1 To perform the task, you need to use the auto-release pool in the method being called 

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


  - (void)run3

  {

  @autoreleasepool {

  NSLog(@" The main thread 3 : %@ , the current thread 3 : %@",[NSThread mainThread],[NSThread currentThread]);

  }

  }

(3) The child thread executes time-consuming operation, and the main thread updates UI. This is the most common example of multithreaded development. The performSelectorOnMainThread method is called from the child thread to update the main thread.


// The test calls the main-thread update in the child thread UI
- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 NSThread *subThread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
 //NSThread You can control the start of a thread 
 [subThread start];
}

- (void)run
{
 NSLog(@" The main thread 1 : %@ , the current thread 1 : %@",[NSThread mainThread],[NSThread currentThread]);
 // The following methods need to be called in the child thread 
 [self performSelectorOnMainThread:@selector(invocationMainThread) withObject:nil waitUntilDone:YES];
}

- (void)invocationMainThread
{
 NSLog(@" The main thread 2 : %@ , the current thread 2 : %@",[NSThread mainThread],[NSThread currentThread]);
 NSLog(@" Call the main thread update UI");
}

(4) Similarly, we can create a new subthread class, inherited from NSThread. Then override the main method inside. main is the method that will be executed when the thread starts.


@implementation MyThread

- (void)main
{
 NSLog(@"main Methods to perform ");
}

@end

Then start with normal creation. The thread will automatically execute the main method.


// You can write it yourself 1 Subclasses, inherited from NSThread , need to be rewritten main methods 
/**
 *  The executed code is in main In, rather than using @selector.
  use main Method, the method executed in the thread belongs to the object itself, so it can be used anywhere else the thread method needs to be used, not again 1 Subimplement a method. 
 
  And the others are direct NSThread The methods executed within the thread are in the current class file. 
 */
- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 MyThread *thread = [[MyThread alloc] init];
 [thread start];
}

(5) Another very common method in NSThread is delay. Delay 2s execution.


 // Thread sleep to simulate time-consuming operations 
 [NSThread sleepForTimeInterval:2];

For the three implementation of multithreading, we must be able to skillfully use


Related articles: