ios-Multi-threading, organizing collections, creating multiple threads

Source: Internet
Author: User

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{        Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];    The first way to create a thread nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run:) object:@ "Universe"];    [Thread start];            [Thread release];            The second way to create a thread, the Nsthread class method [Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Yuzhou"];        The third method of creating a thread NSObject method [self performselectorinbackground: @selector (run:) withobject:@ "NSObject thread"];    The fourth way to create a thread nsoperationqueue *oprationqueue = [[Nsoperationqueue alloc] init];    [Oprationqueue addoperationwithblock:^{//This block statement chunk executes NSLog (@ "Oprationqueue") in the child thread;    }];        [Oprationqueue release];    Fifth ways to create threads nsoperationqueue *oprationqueue1 = [[Nsoperationqueue alloc] init];   Oprationqueue1.maxconcurrentoperationcount = 1;//The number of concurrent numbers for the specified pool     Nsoperation equivalent to the Runnable interface in Java, inherited from it is quite a task nsinvocationoperation *invation = [[Nsinvocationoperation alloc] In    Itwithtarget:self selector: @selector (run:) object:@ "Invation"];        [OprationQueue1 addoperation:invation];//adds a task to the pool and can add multiple tasks to the pool and specifies its concurrency [invation release]; The second task nsinvocationoperation *invation2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (run2    :) object:@ "Invocation2"];    invation2.queuepriority = nsoperationqueuepriorityhigh;//Set Thread priority [OprationQueue1 Addoperation:invation2];        [Invation2 release];        [OprationQueue1 release]; Call the main thread, which is used for the sub-thread and the main thread to interact, the last side of the Boolean parameter, if yes is to wait for the method to execute after the execution of the Code; If no, it is no matter whether this method is finished or not, then go down [self        Performselectoronmainthread: @selector (Onmain) withobject:self Waituntildone:yes]; ---------------------GCD----------------------Support multi-core, high-efficiency multithreading technology//Create multithreading sixth way dispatch_queue_t queue = Dispatch_queue    _create ("name", NULL); Create a child thread Dispatch_async (queue, ^{//Child thread code .....                NSLog (@ "GCD multithreading"); Back to the main thread Dispatch_sync (Dispatch_get_main_queue (), ^{//actually this also executes in a sub-thread, just put it in the main thread of the queue Boolean ismain = [NST            Hread Ismainthread];            if (ismain) {NSLog (@ "GCD main thread");    }        });            });    Self.window.backgroundColor = [Uicolor Whitecolor];    [Self.window makekeyandvisible]; return YES;}    -(void) onmain{Boolean b = [Nsthread ismainthread]; if (b) {NSLog (@ "onmain;;    %d ", b); }}-(void) Run: (nsstring*) str{NSLog (@ "multithreaded run:::%@", str);} -(void) Run2: (nsstring*) str{NSLog (@ "multithreaded run:::%@", str);}



These are basic thread creation, using Nsthread to create threads is simpler, if a single creation thread can be used with nsthread. Direct creation can be used. Simply write the function and method of the thread execution to create a thread.

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.