Network Multithreading for iOS development

Source: Internet
Author: User
Tags gcd

Network Multithreading for iOS development 1. pthred (POSIX) General situation no 2. Nsthread
  1. nsthread *thread1 = [[nsthread alloc] initwithtarget: Self Selector:@selector(dealthreadexit:) Object:nil];
  2. [Thread1 start];
  3. [nsthread detachnewthreadselector:@selector(detailtextlabel) totarget: Self withobject:nil];
  4.  
  5. [[nsnotificationcenter defaultcenter] addobserver:self selector:@ Selector(dealthreadexit:) name:nsthreadwillexitnotification object:nil];
  6. [Thread1 cancel]; // call thread1 to stop.
  7. [Thread1 iscancelled]; // See if Thread1 can stop
  8. [nsthread exit]; // Stop current thread
  9. [Self performselectoronmainthread:<# (SEL) #> withobject:<# (ID) #> waituntildone:<# (BOOL) #>] let this method execute in the main thread

3.NSOperation

 

------------------Nsoperationqueue----------

Execute now

Nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];

[Operationqueue addoperationwithblock:^{

}];

Set the maximum number of concurrent threads for a thread pool

Operationqueue.maxconcurrentoperationcount = 2;

Created a thread that can be put into a thread pool

Nsinvocationoperation *inth1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (thread1:) object : nil];

Set the priority of the thread that is put into the thread pool

inth1.queuepriority = 1;

[Operationqueue addoperation:inth1];

[Nsthread ismainthread];//whether the current thread is the main path

[Nsthread ismultithreaded]; Whether the current thread is

[Nsthread sleepfortimeinterval:1.0]; The current thread sleeps xx seconds

4.GCD

Create a priority task

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);

Start an async asynchronous task

Dispatch_async (Queue, ^{

for (int i=0; i<20; i++) {

NSLog (@ "A =%d", i);

}

});

Start an asynchronous task again

Dispatch_async (Queue, ^{

for (int i=0; i<20; i++) {

NSLog (@ "B =%d", i);

}

});

GCD the simplest way to open an asynchronous task//create an asynchronous thread incoming priority

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

});

Open a task to execute on the main thread

Dispatch_async (Dispatch_get_main_queue (), ^{

});

Open an asynchronous task to execute only once

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

});

Open an asynchronous task Group XXX can control what tasks are performed after all tasks are completed

Open an asynchronous task to execute after xx seconds

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

});

Group Task Group is a task group that can add multiple tasks to him

dispatch_group_t group = Dispatch_group_create ();

Add Task 7s Finish/Task group//Priority Settings/

Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

});

10s complete

Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

});

These tasks are all performed asynchronously by this method to detect when a task group group does not have a task that is being performed .... Task

Dispatch_group_notify (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

NSLog (@ "All tasks performed, Auto Power off");

});

Network Multithreading for iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.