Gcd (1)

Source: Internet
Author: User

'''Objc

Import "viewcontroller. H"

Int threadnumber = 0;

Int newthingnumber = 0;

@ Interface viewcontroller ()

@ End

@ Implementation viewcontroller

  • (Void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib .}

// 1 if the main thread and sub-thread are in parallel, they depend on CPU scheduling, and both the main thread and sub-thread have the opportunity to be executed. // 2 if you want to change the data in the main thread in the Child thread, use the pass-through (void *) & C, otherwise, the purpose of calling (void *) C // 3 using pthread_join is: if the main thread is too fast to execute and the sub-thread is not executed, the main thread is executed and exited.

  • (Ibaction) dosomething :( ID) sender {

// If it is executed in a single thread, the words in the button state will last for 10 seconds. // currently, for iOS, the program GUI is executed in the main thread. In other words, all UI programs should be run in the main thread. // by default, the callback method of the button is running in the main thread.

// [Self dothing: Nil];

// Start a new thread, which means synchronous execution (synchronization) // The first method to start a new thread // The first parameter: Enable the background thread, the Code (task) to be executed. The second parameter is the thread communication parameter.

// [Self defined mselectorinbackground: @ selector (dothing :) withobject: @ "hellowangdelong"];

// Method 2 for enabling threads

// Start a subthread and use dothing: As the thread entry point (thread task) // The second parameter: specifies which object defines the thread entry point (thread job), // The third parameter: data passed to the sub-thread (the means to implement thread Communication)

// [Nsthread detachnewthreadselector: @ selector (dothing :) totarget: Self withobject: @ "hello"];

// Method 3 for enabling a thread // create a thread object to control when the thread starts running.

// Nsthread * thread = [[nsthread alloc] initwithtarget: Self selector: @ selector (dothing :) object: @ "Qingyun"]; // name the thread, used to identify this thread. // Thread. Name = @ "firstqingyunthread ";

// Start the thread

// [Thread start];

// Method 4 for enabling threads // create a queue

// Nsoperationqueue * queque = [[nsoperationqueue alloc] init]; // create a subtask, define a subtask as a subclass of nsoperation // nsinvocationoperation * operation = [[nsinvocationoperation alloc] initwithtarget: Self selector: @ selector (dothing :) object: @ "hellooperationqueue"]; // when a task is added to the queue, the thread is automatically enabled. // [queque addoperation: Operation]; // the same task object appears in different nsoperationqueue queues, otherwise, an exception may occur. // [queque addoperation: Operation];

// Method 5 for enabling a thread

// Nsoperationqueue * queque = [[nsoperationqueue alloc] init]; // nsblockoperation * Handler = [nsblockoperation blockoperationwithblock: ^ {// nslog (@ "do thing... "); // threadnumber ++; // indicates the thread object of dothing execution. // nsthread * currentthread = [nsthread currentthread]; //
//// Ismainthread: If it is the main thread, yes is returned; otherwise, no // If ([currentthread ismainthread]) {// nslog (@ "main thread... % d ", threadnumber); //} else // {// nslog (@" peer thread... % d ", threadnumber );//}//
// [Nsthread sleepfortimeinterval: 10]; // sleep the processor for 10 seconds // nslog (@ "Finish dothing ");//
//}]; //
// [Queque addoperation: queue]; //

// What is the difference between the sixth Method for enabling a thread // GCD // serial Queue (null, nil nsnull? The first parameter uniquely identifies the created queue object
If 0
dispatch_queue_t queue = dispatch_queue_create("wangdelong", NULL);

// Asynchronously execute the GCD block (thread task) // 1? For the serial queue, the main thread can be understood as a queue. the queue and queue must be executed asynchronously. // 2? For sub-tasks in the same serial queue, the sub-tasks are executed synchronously and follow the FIFO rules.

Dispatch_async (queue, ^ {nslog (@ "dothing... "); threadnumber ++; // indicates nsthread * currentthread = [nsthread currentthread], the thread object currently executing dothing; // if it is executed in the main thread, it is printed in the main thread. // Otherwise, the print is the peer thread // determine whether it is the main thread or the peer thread (asynchronous) if ([currentthread ismainthread]) {nslog (@ "main thread is: % d ", threadnumber);} else {nslog (@" peer thread ID: % d ", threadnumber);} // Let the processor sleep for 10 seconds [nsthread sleepfortimeinterval: 5]; nslog (@ "finish working ");});

// DispatchQueueT secondqueue = DispatchQueueCreate ("wangdelong", null );

Dispatch_async (queue, ^ {nslog (@ "dothing... "); threadnumber ++; // indicates nsthread * currentthread = [nsthread currentthread], the thread object currently executing dothing; // if it is executed in the main thread, it is printed in the main thread. // Otherwise, the print is the peer thread // determine whether it is the main thread or the peer thread (asynchronous) if ([currentthread ismainthread]) {nslog (@ "secondmain thread is: % d ", threadnumber) ;}else {nslog (@" secondpeer thread ID: % d ", threadnumber) ;}/// let the processor sleep for 10 seconds [nsthread sleepfortimeinterval: 5]; nslog (@ "second finish working ");});
Endif

// Concurrent queues run concurrently among multiple subtasks in the same Queue (note the difference between this and serial Queue) // different subtasks in the same queue are concurrently executed. // different queues are scheduled based on their priorities. If the priorities are high, they are first scheduled. // The first parameter: used to identify the priority of the queue, which can be: // # define dispatchQueuePriorityHigh // # define dispatchQueuePriorityDefault // # define dispatchQueuePriorityLow // # define dispatchQueuePriorityBackground // The second parameter: it can be set to 0 and is currently retained.

Dispatch_queue_t queue = Queue (queue, 0); dispatch_queue_t secondqueue = dispatch_get_global_queue (queue, 0); dispatch_async (queue, ^ {nslog (@ "first dothing... "); threadnumber ++; // indicates nsthread * currentthread = [nsthread currentthread], the thread object currently executing dothing; // if it is executed in the main thread, it is printed in the main thread. // Otherwise, the print is the peer thread // determine whether it is the main thread or the peer thread (asynchronous) if ([currentthread ismainthread]) {nslog (@ "first main thread is: % d ", threadnumber);} else {nslog (@" first peer thread ID: % d ", threadnumber);} // Let the processor sleep for 10 seconds [nsthread sleepfortimeinterval: 5]; nslog (@ "first finish working") ;}); dispatch_sync (secondqueue, ^ {nslog (@ "second dothing... "); threadnumber ++; // indicates the nsthread * currentthread = [nsthread curre Ntthread]; // if it is executed in the main thread, print it in the main thread. // Otherwise, the print is the peer thread // determine whether it is the main thread or the peer thread (asynchronous) if ([currentthread ismainthread]) {nslog (@ "second main thread is: % d ", threadnumber);} else {nslog (@" second peer thread ID: % d ", threadnumber);} // sleep the processor for 10 seconds [nsthread sleepfortimeinterval: 5]; nslog (@ "second finish working") ;}); // indicates the nsthread * currentthread = [nsthread currentthread] of the thread object currently executing dothing; // ismainthread: if it is the main thread, yes is returned, otherwise noif ([currentthread ismainthread]) {nslog (@ "this is main thread... % d ", threadnumber);} else {nslog (@" this is peer thread... % d ", threadnumber );}

}

-(Void) dothing :( ID) object {

NSLog(@"doThing...:%@",object);threadNumber++;

// Indicates nsthread * currentthread = [nsthread currentthread], the thread object currently executing dothing;

// If it is executed in the main thread, it is printed in the main thread. // Otherwise, the print is the peer thread // determine whether it is the main thread or the peer thread (asynchronous) if ([currentthread ismainthread]) {nslog (@ "main thread is: % d ", threadnumber );

}else{    NSLog(@"Peer thread id :%d",threadNumber);}

/// Sleep the processor for 10 seconds [nsthread sleepfortimeinterval: 10];

NSLog(@"finish working");

}

// Redefine a button,-(ibaction) donewthing :( ID) sender {

NSLog(@"do another thing...:%d",newThingNumber);newThingNumber++;if ([NSThread isMainThread]) {    NSLog(@"do another thing is main thread");}else{    NSLog(@"do another thing is peer thread");}[NSThread sleepForTimeInterval:3];NSLog(@"do another thing finished");

}

@ End

'''

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.