Multithreading-Simple Interest
+ (Singlehandel *) Sharemodel
{
Static dispatch_once_t Oncequeue;
Dispatch_once (&oncequeue, ^{
Sharesingle = [[Singlehandel alloc] init];
});
return sharesingle;
}
/*//First thread open mode
Nsthread *thread1 = [[Nsthread alloc] initwithtarget:self selector: @selector (threadaction) Object:nil];
Give your thread a name.
Thread1.name = @ "Thread1";
Start thread
[Thread1 start];
Shut down
[Thread1 cancel];*/
The second way to open the thread (the thread is automatically opened when the program is running)
/*[nsthread detachnewthreadselector: @selector (threadaction) totarget:self withobject:nil];*/
The third kind of inheritance and Nsobjet
[Self Performselectorinbackground: @selector (threadaction) Withobject:nil];
Nsoperation
Nsinvocationoperation *inop = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (invocation) Object:nil];
Nsblockoperation *blockop = [Nsblockoperation blockoperationwithblock:^{
NSLog (@ "I am block");
}];
[Blockop start];
Create a queue
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
Set the maximum number of simultaneous executions
Queue.maxconcurrentoperationcount = 1;
Add Event
[Queue Addoperation:inop];
Sleep (1);
[Queue Addoperation:blockop];
}
-(void) invocation
{
NSLog (@ "%@", [Nsthread CurrentThread]);
NSLog (@ "I am the Method");
}
/-----------------------------------------------------GCD---------------------------------------------------------------- /
Multithreading---GCD
-(Ibaction) Buttonaction: (ID) sender
{
GCD (FIFO first-in-out abbreviation)
After a serial execution completes, the latter task can execute
Parallel tasks are ordered at the time of dispatch without waiting for the first task to complete before starting the next one (that is, a piece of execution)
GCD is divided into three types: primary queue, Global queue, custom queue
/*--------------------home Row (serial)----------------------*/
Use the primary queue for task dispatch (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 (@ "The third task:%@", [Nsthread CurrentThread]);
});*/
/*--------------------Custom Queue (serial)----------------------*/
Custom queues
/* 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 (@ "The 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, ^{
Can delay
Sleep (3);
NSLog (@ "second task%@", [Nsthread CurrentThread]);
});
Dispatch_async (Myqueue1, ^{
NSLog (@ "The 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 (@ "The third task:%@", [Nsthread CurrentThread]);
});*/
/*---------------------------------Delay---------------------------------------------*/
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (4 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
[Self afther];
});
/*-----------------------------Repeat---------------------------------------------*/
Dispatch_apply (3, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^ (size_t t) {
[Self afther];
});
/*-----------------------------Perform once---------------------------------------------*/
Static dispatch_once_t Oncequeue;
Dispatch_once (&oncequeue, ^{
NSLog (@ "I'll do it once");
});
/*----------------------------------Thread communication---------------------------------*/
Encounter thread communication, execute his method, can go back to the main thread (if you are executing a child thread, this method can jump out of the child thread to the main thread)
[Nsthread detachnewthreadselector: @selector (newthread) totarget:self Withobject:nil];
}
-(void) Newthread
{
NSLog (@ "I am a Child thread");
Back to main thread
[Self Performselectoronmainthread: @selector (afther) withobject:self Waituntildone:no];
}
-(void) afther
{
NSLog (@ "sad");
}
IOS multithreaded/GCD