Sometimes there may be a lot of features to be implemented at the same time, such as how often the program network connection, or sometimes need to download a small file from the server, if the use of single-threaded is almost impossible, the program will not be used, multi-threaded and not multi-threaded, to the user experience, So multithreading is a point of knowledge that an iOS developer must learn.
Multithreading, listening to a bit advanced, in fact, very simple. In the iphone, multithreading can understand that, in addition to the main thread, there are other threads, the main thread and other threads of the difference is the most important, the simplest way to understand is that the main thread will change the interface, the other threads will not change, the main thread can call other threads,
1. Declare a thread in one of two ways: 1.thread1=[[nsthread alloc]initwithtarget:self selector: @selector (TT1) Object:nil]; This method must start [Thread1 Start];
2.[nsthread detachnewthreadselector: @selector (TT1) totarget:self Withobject:nil]; This method does not need to declare nsthread objects and does not need to be started. The default execution to this sentence is run directly
2. When the program executes in the background, it may sometimes be necessary to invoke another method named "Self Performselectorinbackground: @Selector (threadmethod) Withobject:nil]
3. Set Thread Priority – (void) SetThreadPriority: (double) priority; thread precedence from 0.0 to 1.0,1.0 is the highest priority. The system default priority is 0.5
4. Thread hibernation: + (void) Sleepfortimeinterval: (nstimeinterval) The Time;time parameter is the sleep time.
5. Thread interaction: During the run of the thread, you may need to communicate with other threads, such as modifying the interface in the main thread, and so on, you can use the following interface:-(void) Performselectoronmainthread: (SEL) Selectoe Withobject: (ID) arg waituntildone: (bool) wait;
6. Thread synchronization and thread locking, you can understand the following
7. Thread pool, you can understand the following
Using multithreading to implement a simple program
#import <UIKit/UIKit.h>
@interface Toolbar_1viewcontroller:uiviewcontroller {
UILabel * L1;
UILabel * L2;
UILabel * L3;
UILabel * L4;
UILabel * L5;
UIButton *b1;
UIButton *b2;
UIButton *b3;
UIButton *b4;
UIButton *b5;
Nsthread *thread1;
Nsthread *thread2;
Nsthread *thread3;
Nsthread *thread4;
BOOL button1;
BOOL Button2;
BOOL Button3;
BOOL Button4;
BOOL Button5;
int i1;
int i2;
int i3;
int I4;
int i5;
Nscondition *lock;
}
@property (nonatomic,retain) iboutlet UILabel *l1;
@property (nonatomic,retain) iboutlet UILabel *l2;
@property (nonatomic,retain) iboutlet UILabel *l3;
@property (nonatomic,retain) iboutlet UILabel *l4;
@property (nonatomic,retain) iboutlet UILabel *l5;
@property (nonatomic,retain) iboutlet UIButton *b1;
@property (nonatomic,retain) iboutlet UIButton *b2;
@property (nonatomic,retain) iboutlet UIButton *b3;
@property (nonatomic,retain) iboutlet UIButton *b4;
@property (nonatomic,retain) iboutlet UIButton *b5;
-(Ibaction) B1: (ID) sender;
-(Ibaction) B2: (ID) sender;
-(Ibaction) B3: (ID) sender;
-(Ibaction) B4: (ID) sender;
-(Ibaction) B5: (id) sender;
-(void) T1;
-(void) tt1;
-(void) T2;
-(void) tt2;
-(void) T3;
-(void) tt3;
-(void) T4;
-(void) tt4;
-(void) Changeall;
@end
#import "Toolbar_1viewcontroller.h"
@implementation Toolbar_1viewcontroller
@synthesize L1;
@synthesize L2;
@synthesize L3;
@synthesize L4;
@synthesize L5;
@synthesize B1;
@synthesize B2;
@synthesize B3;
@synthesize b4;
@synthesize b5;
-(Ibaction) B1: (ID) sender
{
Thread1=[[nsthread alloc]initwithtarget:self selector: @selector (TT1) Object:nil];
Button1=!button1;
if (button1) {
[B1 settitle:@ "Stop 1" forstate:uicontrolstatenormal];
[Self changeall];
[Thread1 start];
}
else {
[B1 settitle:@ "Start 1" forstate:uicontrolstatenormal];
[Self changeall];
[Thread1 Cancel];
}
}
-(void) Changeall
{
if (button1&&button2&button3&button4) {
[B5 settitle:@ "Stop All" forstate:uicontrolstatenormal];
Button5=yes;
}
if (!button1&&!button2&!button3&!button4) {
[B5 settitle:@ "Start All" forstate:uicontrolstatenormal];
Button5=no;
}
}
-(void) TT1
{
while (button1) {
[Self performselectoronmainthread: @selector (T1) Withobject:nil Waituntildone:no];
[Nsthread sleepfortimeinterval:1.0];
}
}
-(void) T1
{
L1.text=[nsstring stringwithformat:@ "%d", ++i1];
[Lock lock];
i5 + +;
[Lock unlock];
L5.text=[nsstring stringwithformat:@ "%d", i5];
[Self.view Setneedsdisplay];
}
-(Ibaction) B2: (ID) sender
{
Thread2=[[nsthread alloc]initwithtarget:self selector: @selector (TT2) Object:nil];
Button2=!button2;
if (button2) {
[B2 settitle:@ "Stop 2" forstate:uicontrolstatenormal];
[Self changeall];
[Thread2 start];
}
else {
[B2 settitle:@ "Start 2" forstate:uicontrolstatenormal];
[Self changeall];
[Thread2 Cancel];
}
}
-(void) TT2
{
while (Button2) {
[Self performselectoronmainthread: @selector (T2) Withobject:nil Waituntildone:no];
[Nsthread sleepfortimeinterval:1.0];
}
}
-(void) T2
{
L2.text=[nsstring stringwithformat:@ "%d", ++i2];
[Lock lock];
i5 + +;
[Lock unlock];
L5.text=[nsstring stringwithformat:@ "%d", i5];
[Self.view Setneedsdisplay];
}
-(Ibaction) B3: (ID) sender
{
Thread3=[[nsthread alloc]initwithtarget:self selector: @selector (TT3) Object:nil];
Button3=!button3;
if (Button3) {
[B3 settitle:@ "Stop 3" forstate:uicontrolstatenormal];
[Self changeall];
[Thread3 start];
}
else {
[B3 settitle:@ "Start 3" forstate:uicontrolstatenormal];
[Self changeall];
[Thread3 Cancel];
}
}
-(void) tt3
{
while (Button3) {
[Self performselectoronmainthread: @selector (T3) Withobject:nil Waituntildone:no];
[Nsthread sleepfortimeinterval:1.0];
}
}
-(void) T3
{
L3.text=[nsstring stringwithformat:@ "%d", ++i3];
[Lock lock];
i5 + +;
[Lock unlock];
L5.text=[nsstring stringwithformat:@ "%d", i5];
[Self.view Setneedsdisplay];
}
-(Ibaction) B4: (ID) sender
{
Thread4=[[nsthread alloc]initwithtarget:self selector: @selector (TT4) Object:nil];
Button4=!button4;
if (button4) {
[B4 settitle:@ "Stop 4" forstate:uicontrolstatenormal];
[Self changeall];
[Thread4 start];
}
else {
[B4 settitle:@ "Start 4" forstate:uicontrolstatenormal];
[Self changeall];
[Thread4 Cancel];
}
}
-(void) TT4
{
while (Button4) {
[Self performselectoronmainthread: @selector (T4) Withobject:nil waituntildone:no];
[Nsthread sleepfortimeinterval:1.0];
}
}
-(void) T4
{
L4.text=[nsstring stringwithformat:@ "%d", ++i4];
[Lock lock];
i5 + +;
[Lock unlock];
L5.text=[nsstring stringwithformat:@ "%d", i5];
[Self.view Setneedsdisplay];
}
-(Ibaction) B5: (ID) sender
{
if (!BUTTON5) {
[B5 settitle:@ "Stop All" forstate:uicontrolstatenormal];
if (!button1) {
Thread1=[[nsthread alloc]initwithtarget:self selector: @selector (TT1) Object:nil];
Button1=!button1;
[B1 settitle:@ "Stop 1" forstate:uicontrolstatenormal];
[Thread1 start];
}
if (!button2) {
Thread2=[[nsthread alloc]initwithtarget:self selector: @selector (TT2) Object:nil];
Button2=!button2;
[B2 settitle:@ "Stop 2" forstate:uicontrolstatenormal];
[Thread2 start];
}
if (!button3) {
Thread3=[[nsthread alloc]initwithtarget:self selector: @selector (TT3) Object:nil];
Button3=!button3;
[B3 settitle:@ "Stop 3" forstate:uicontrolstatenormal];
[Thread3 start];
}
if (!button4) {
Thread4=[[nsthread alloc]initwithtarget:self selector: @selector (TT4) Object:nil];
Button4=!button4;
[B4 settitle:@ "Stop 4" forstate:uicontrolstatenormal];
[Thread4 start];
}
}
else{
[B5 settitle:@ "Start All" forstate:uicontrolstatenormal];
if (button1) {
Button1=!button1;
[B1 settitle:@ "Start 1" forstate:uicontrolstatenormal];
[Thread1 Cancel];
}
if (button2) {
Button2=!button2;
[B2 settitle:@ "Start 2" forstate:uicontrolstatenormal];
[Thread2 Cancel];
}
if (Button3) {
Button3=!button3;
[B3 settitle:@ "Start 3" forstate:uicontrolstatenormal];
[Thread3 Cancel];
}
if (button4) {
Button4=!button4;
[B4 settitle:@ "Start 4" forstate:uicontrolstatenormal];
[Thread4 Cancel];
}
}
Button5=!button5;
}
-(void) dealloc
{
[L1 release];
[L2 release];
[L3 release];
[L4 release];
[L5 release];
[B1 release];
[B2 release];
[B3 release];
[B4 release];
[B5 release];
[Thread1 release];
[Thread2 release];
[Thread3 release];
[Thread4 release];
[Super Dealloc];
}
-(void) viewdidload
{
i1=0;
Button1=false;
Button2=false;
Button3=false;
Button4=false;
Button5=false;
I5=I2=I3=I4=I1;
L1.text=[nsstring stringwithformat:@ "%d", i1];
L2.text=[nsstring stringwithformat:@ "%d", i2];
L3.text=[nsstring stringwithformat:@ "%d", i3];
L4.text=[nsstring stringwithformat:@ "%d", I4];
L5.text=[nsstring stringwithformat:@ "%d", i5];
}
@end
Xib files are as follows
iOS Development/iphone Developing multithreading