First, the initialization of Nsthread
1. Dynamic method
Copy Code code as follows:
-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument;
Initializing threads
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
Set the priority of a thread (0.0-1.0,1.0-highest)
thread.threadpriority = 1;
Open Thread
[Thread start];
Parameter resolution:
Selector: The method that the thread executes, this selector can receive at most one parameter
Target:selector the object sent by the message
Argument: The only parameter passed to selector, or it can be nil
2. Static method
Copy Code code as follows:
+ (void) Detachnewthreadselector: (SEL) selector totarget: (ID) target withobject: (id) argument;
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
When the call is complete, a new thread is created and opened immediately
3. Ways to create threads implicitly
Copy Code code as follows:
[Self Performselectorinbackground: @selector (Run) Withobject:nil];
Second, get the current thread
Copy Code code as follows:
Nsthread *current = [Nsthread CurrentThread];
Third, get the main thread
Copy Code code as follows:
Nsthread *main = [Nsthread mainthread];
Pausing the current thread
Copy Code code as follows:
Pause 2s
[Nsthread Sleepfortimeinterval:2];
Or
NSDate *date = [nsdate datewithtimeinterval:2 sincedate:[nsdate Date]];
[Nsthread Sleepuntildate:date];
V. Communication between threads
1. Perform actions on the specified thread
Copy Code code as follows:
[Self performselector: @selector (run) onthread:thread Withobject:nil Waituntildone:yes];
2. Performing operations on the main thread
Copy Code code as follows:
[Self performselectoronmainthread: @selector (Run) Withobject:nil Waituntildone:yes];
3. Performing operations on the current thread
Copy Code code as follows:
[Self performselector: @selector (Run) Withobject:nil];
Vi. Advantages and Disadvantages
1. Advantages: Nsthread More lightweight than the other two multithreaded schemes, more intuitive control of thread objects
2. Disadvantage: You need to manage the thread's lifecycle, thread synchronization. Thread synchronization has a certain overhead for locking data
Seven, download the picture example:
New Singeview App
Creates a new project and places a ImageView control on the Xib file. Hold down the control key and drag to Viewcontroll
Create ImageView iboutlet in Er.h file
Implemented in VIEWCONTROLLER.M:
Copy Code code as follows:
//
Viewcontroller.m
Nsthreaddemo
//
Created by Rongfzh on 12-9-23.
Copyright (c) 2012 Rongfzh. All rights reserved.
//
#import "ViewController.h"
#define KURL @ "Http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
@interface Viewcontroller ()
@end
Copy Code code as follows:
@implementation Viewcontroller
-(void) Downloadimage: (NSString *) url{
NSData *data = [[NSData alloc] Initwithcontentsofurl:[nsurl Urlwithstring:url]];
UIImage *image = [[UIImage alloc]initwithdata:data];
if (image = = nil) {
}else{
[Self Performselectoronmainthread: @selector (updateui:) withobject:image Waituntildone:yes];
}
}
-(void) UpdateUI: (uiimage*) image{
Self.imageView.image = image;
}
-(void) viewdidload
{
[Super Viewdidload];
[Nsthread detachnewthreadselector: @selector (downloadimage:) totarget:self Withobject:kurl];
Nsthread *thread = [[Nsthread alloc]initwithtarget:self selector: @selector (downloadimage:) Object:kurl];
[Thread start];
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
@end
Communication between threads
How do I inform the main thread update interface after the thread downloads the picture?
Copy Code code as follows:
[Self Performselectoronmainthread: @selector (updateui:) withobject:image Waituntildone:yes];
Performselectoronmainthread is a NSObject method, in addition to updating the main thread's data, you can update other threads such as:
Use:
Copy Code code as follows:
PerformSelector:onThread:withObject:waitUntilDone:
To run the download Picture:
The picture is downloaded.