<span id="Label3"></p><p><p><span style="font-size: 16px;">-(void) Viewdidload {</span></p></p><p><p><span style="font-size: 16px;">[super viewdidload];</span></p></p><p><p><span style="font-size: 16px;">/* process: Running Program thread: the basic execution unit of a process, where the task is executed</span></p></p><p><p><span style="font-size: 16px;">Thread Serial: 1 threads in the execution of a task serially, at the same time, 1 threads can only perform one task</span></p></p><p><p><span style="font-size: 16px;">Multithreading: multiple threads can be opened in a process, the principle is that the CPU is quickly dispatched between multiple threads (switching), more CPU</span></p></p><p><p><span style="font-size: 16px;">Main Thread/ui threads: display/refresh/process ui, Place time-consuming processing on child threads */</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">/* Create Thread</span></p></p><p><p><span style="font-size: 16px;">To manually start a thread: [thread start] then the task is Executed.</span></p></p><p><p><span style="font-size: 16px;">Nsthread *thread = [[nsthread alloc]initwithtarget:self selector: @selector (text) object:nil];</span></p></p><p><p><span style="font-size: 16px;">To start a thread automatically:</span></p></p><p><p><span style="font-size: 16px;">[nsthread detachnewthreadselector: @selector (text) totarget:withobiect:]</span></p></p><p><p><span style="font-size: 16px;">Implicitly created and Started:</span></p></p><p><p><span style="font-size: 16px;">[self performselectorinbackground: @selector (text) withobject:]</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">Get the main thread</span></p></p><p><p><span style="font-size: 16px;">Nsthread *mainthread = [nsthread mainthread];</span></p></p><p><p><span style="font-size: 16px;">Determine if the main thread</span></p></p><p><p><span style="font-size: 16px;">BOOL *b = [mainthread ismainthread];</span></p></p><p><p><span style="font-size: 16px;">Gets the current thread</span></p></p><p><p><span style="font-size: 16px;">Nsthread *currentthread = [nsthread currentthread];</span></p></p><p><p><span style="font-size: 16px;">Priority of Thread scheduling</span></p></p><p><p><span style="font-size: 16px;">+ (double) threadpriority;</span></p></p><p><p><span style="font-size: 16px;">+ (BOOL) setthreadpriority: (double) p;</span></p></p><p><p><span style="font-size: 16px;">Set the name of the thread</span></p></p><p><p><span style="font-size: 16px;">-(void) setName: (nsstring *) n;</span></p></p><p><p><span style="font-size: 16px;">-(nsstring *) name;</span></p></p><p><p><span style="font-size: 16px;">Exit Thread: +exit</span></p></p><p><p><span style="font-size: 16px;">*/</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">/* thread safety: A resource may be accessed by multiple threads, causing data confusion and data security issues</span></p></p><p><p><span style="font-size: 16px;">Fix: thread synchronization technology, mutex @synchronized (lock Object) {//code required to lock}</span></p></p><p><p><span style="font-size: 16px;">Only one thread can access a time period until the thread is finished</span></p></p><p><p><span style="font-size: 16px;">Atomic: Atomic properties, linear security, locking for setter methods</span></p></p><p><p><span style="font-size: 16px;">*/</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">/* communication between threads: transferring data between lines, passing tasks</span></p></p><p><p><span style="font-size: 16px;">-(void) performSelectorOnMainThread:withObject:waitUntilDone:</span></p></p><p><p><span style="font-size: 16px;">-(void) performSelector:onThread:withObiect:waitUntilDone:</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">*/</span></p></p><p><p><span style="font-size: 16px;">/* thread blockage (call The sleep method or a synchronous lock, at which point the thread cannot be dispatched):</span></p></p><p><p><span style="font-size: 16px;">The first kind [nsthread sleepfortimeinterval:2.0];</span></p></p><p><p><span style="font-size: 16px;">The second is based on the current time as a benchmark nsdate *date = [nsdate datewithtimeintervalsincenow:4.0];</span></p></p><p><p><span style="font-size: 16px;">[nsthread sleepuntildate:date];</span></p></p><p><p><span style="font-size: 16px;">Thread Death: An exception was forced to quit when the task ended</span></p></p><p><p><span style="font-size: 16px;">The thread is created, but the thread does not open immediately!!!!</span></p></p><p><p><span style="font-size: 16px;">*/</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">-(void) didreceivememorywarning {</span></p></p><p><p><span style="font-size: 16px;">[super didreceivememorywarning];</span></p></p><p><p><span style="font-size: 16px;">Dispose of any resources the can be Recreated.</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">-(void) touchesbegan: (nsset *) touches withevent: (uievent *) Event</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">[self performselectorinbackground: @selector (download) withobject:nil];</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">-(void) Download</span></p></p><p><p><span style="font-size: 16px;">{//get a picture</span></p></p><p><p><span style="font-size: 16px;">NSString *imagestrring = [nsstring stringwithformat:@ "http://i6.topit.me/6/5d/45/1131907198420455d6o.jpg"];</span></p></p><p><p><span style="font-size: 16px;">Nsurl *imageurl = [nsurl urlwithstring:imagestrring];</span></p></p><p><p><span style="font-size: 16px;">NSData *imagedata = [nsdata datawithcontentsofurl:imageurl];</span></p></p><p><p><span style="font-size: 16px;">UIImage *image = [UIImage imagewithdata:imagedata];</span></p></p><p><p><span style="font-size: 16px;">Return main thread The second is the parameter to be passed to this method</span></p></p><p><p><span style="font-size: 16px;">[self performselectoronmainthread: @selector (settingimage:) withobject:image waituntildone:no];</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">-(void) settingimage: (UIImage *) image</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">Self.imageView.image = image;</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">//</span></p></p><p><p><span style="font-size: 16px;">-(ibaction) buttonclicked: (id) Sender {</span></p></p><p><p><span style="font-size: 16px;">Nsthread *currentthread = [nsthread currentthread];</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "currentthread----%@", currentthread);</span></p></p><p><p><span style="font-size: 16px;">Nsthread *mainthread = [nsthread mainthread];</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "mainthread-----%@", mainthread);</span></p></p><p><p><span style="font-size: 16px;">[self creatnewthread];</span></p></p><p><p><span style="font-size: 16px;">//</span></p></p><p><p><span style="font-size: 16px;">//}</span></p></p><p><p><span style="font-size: 16px;">-(void) Creatnewthread</span></p></p><p><p><span style="font-size: 16px;">//{</span></p></p><p><p><span style="font-size: 16px;">Nsthread *newthread = [[nsthread alloc]initwithtarget:self selector: @selector (run) object:nil];</span></p></p><p><p><span style="font-size: 16px;">Newthread.name = @ "thread a";</span></p></p><p><p><span style="font-size: 16px;">[newthread start];</span></p></p><p><p><span style="font-size: 16px;">//}</span></p></p><p><p><span style="font-size: 16px;">-(void) Run</span></p></p><p><p><span style="font-size: 16px;">{nsthread *current = [nsthread currentthread];</span></p></p><p><p><span style="font-size: 16px;">For (int i=0; i<100; I++) {</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "run---%@", current);</span></p></p><p><p><span style="font-size: 16px;">// }</span></p></p><p><p><span style="font-size: 16px;">//}</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p>iOS Development multithreading</p></p></span>
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