IOS multithreading/GCD
Multithreading-Shan Li
+ (SingleHandel *) Modeling Model
{
Static dispatch_once_t onceQueue;
Dispatch_once (& onceQueue, ^ {
ShareSingle = [[SingleHandel alloc] init];
});
Return shareSingle;
}
/* // The first thread enabling Method
NSThread * thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (threadAction) object: nil];
// Name your thread
Thread1.name = @ "thread1 ";
// Start the thread
[Thread1 start];
// Close
[Thread1 cancel]; */
// Second thread enabling mode (the thread is automatically enabled when the program is running)
/* [NSThread detachNewThreadSelector: @ selector (threadAction) toTarget: self withObject: nil]; */
// Inheritance and NSObjet
// [Self defined mselectorinbackground: @ selector (threadAction) withObject: nil];
// NSOperation
NSInvocationOperation * inOp = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (invocation) object: nil];
NSBlockOperation * blockOp = [NSBlockOperation blockOperationWithBlock: ^ {
NSLog (@ "I Am a block ");
}];
// [BlockOp start];
// Create a queue
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
// Set the maximum number of concurrent executions
Queue. maxConcurrentOperationCount = 1;
// Add an event
[Queue addOperation: inOp];
// Sleep (1 );
[Queue addOperation: blockOp];
}
-(Void) invocation
{
// NSLog (@ "% @", [NSThread currentThread]);
NSLog (@ "I Am a method ");
}
/------------------------------------------------------- GCD ----------------------------------------------------------------/
Multithreading --- GCD
-(IBAction) buttonAction :( id) sender
{
// GCD (FIFO)
// The last task can be executed only after the previous serial execution is complete.
// Parallel tasks are distributed in an orderly manner. You do not need to wait until the execution of the first task is completed before starting the next task (that is, one execution)
// There are three types of GCD: main queue, global queue, and custom queue.
/* -------------------- Main queue (Serial )----------------------*/
// Use the main queue for Task Distribution (Serial), in the main thread
/* Dispatch_queue_t mainQueue = dispatch_get_main_queue ();
// Add a task
Dispatch_async (mainQueue, ^ {
NSLog (@ "first task % @", [NSThread currentThread]);
});
Dispatch_async (mainQueue, ^ {
NSLog (@ "second task: % @", [NSThread currentThread]);
});
Dispatch_async (mainQueue, ^ {
NSLog (@ "third task: % @", [NSThread currentThread]);
});*/
/* -------------------- Custom Queue (Serial )----------------------*/
// Custom queue
/* Dispatch_queue_t myQueue = dispatch_queue_create ("com. myqueue", DISPATCH_QUEUE_SERIAL );
// Add a task
Dispatch_async (myQueue, ^ {
NSLog (@ "first task % @", [NSThread currentThread]);
});
Dispatch_async (myQueue, ^ {
Sleep (3 );
NSLog (@ "second task: % @", [NSThread currentThread]);
});
Dispatch_async (myQueue, ^ {
NSLog (@ "third task: % @", [NSThread currentThread]);
});*/
/* -------------------- Custom Queue (parallel )----------------------*/
/* Dispatch_queue_t myqueue1 = dispatch_queue_create ("com. s", DISPATCH_QUEUE_CONCURRENT );
Dispatch_async (myqueue1, ^ {
NSLog (@ "first task % @", [NSThread currentThread]);
});
Dispatch_async (myqueue1, ^ {
// Latency
Sleep (3 );
NSLog (@ "second task % @", [NSThread currentThread]);
});
Dispatch_async (myqueue1, ^ {
NSLog (@ "third task % @", [NSThread currentThread]);
});*/
/* -------------------- Global Queue (parallel )----------------------*/
/* Dispatch_queue_t globalQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_async (globalQueue, ^ {
Sleep (2 );
NSLog (@ "first task: % @", [NSThread currentThread]);
});
Dispatch_async (globalQueue, ^ {
NSLog (@ "second task: % @", [NSThread currentThread]);
});
Dispatch_async (globalQueue, ^ {
NSLog (@ "third task: % @", [NSThread currentThread]);
});*/
/* --------------------------------- Delay ---------------------------------------------*/
Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (4 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {
[Self afther];
});
/* ----------------------------- Run the command repeatedly ---------------------------------------------*/
Dispatch_apply (3, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ (size_t t ){
[Self afther];
});
/* ----------------------------- Run the command once ---------------------------------------------*/
Static dispatch_once_t onceQueue;
Dispatch_once (& onceQueue, ^ {
NSLog (@ "I will execute it once ");
});
/* ---------------------------------- Thread communication ---------------------------------*/
// When thread communication occurs, execute his method and return to the main thread (if the sub-thread is being executed, this method can jump out of the sub-thread to the main thread)
[NSThread detachNewThreadSelector: @ selector (newThread) toTarget: self withObject: nil];
}
-(Void) newThread
{
NSLog (@ "I Am a subthread ");
// Return to the main thread
[Self defined mselecw.mainthread: @ selector (afther) withObject: self waitUntilDone: NO];
}
-(Void) afther
{
NSLog (@ "sad ");
}