IOS开发/iphone开发多线程

时间:2023-03-08 16:04:11
IOS开发/iphone开发多线程

有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的体验天壤之别,所以多线程是一个ios开发人员必须学会的一个知识点。

多线程,听得有点高深,其实很简单。在iphone中,多线程可以这么理解,除主线程外还有其他线程,主线程和其他线程的区别是最重要的,最简单的理解方法就是主线程会改变界面,其他线程不会改变,主线程可以调用其他线程,

1.声明一个线程又两种方法:1.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(tt1) object:nil];这种方法必须启动 [thread1 start];
                                  2.[NSThread detachNewThreadSelector:@selector(tt1) toTarget:self withObject:nil];这种方法不需要声明NSThread对象,也不需要启动,默认执行到这句就直接运行了

2.当程序在后台执行时,可能有时候需要调用另外的方法,这个方法名是[self performSelectorInBackground:@Selector(threadMethod) withObject:nil]

3.设置线程优先级-(void)setThreadPriority:(double)priority;线程优先级是从0.0到1.0,1.0是最高优先级。系统默认的优先级是0.5

4.线程休眠:+(void)sleepForTimeInterval:(NSTimeInterval)time;time参数为休眠时间。

5.线程的交互:线程的运行过程中,可能需要与其他线程进行通信,如在主线程中修改界面等等,可以使用如下接口:-(void)performSelectorOnMainThread:(SEL)Selectoe   withObject:(id)arg  waitUntilDone:(bool)wait;

6.线程的同步和线程琐,可以了解下

7.线程池,可以了解下

用多线程实现一个简单的程序
IOS开发/iphone开发多线程

#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:@"停止1" forState:UIControlStateNormal] ;
        [self changeAll];
        [thread1 start];
    }
    else {
        [b1 setTitle:@"启动1" forState:UIControlStateNormal] ;
        [self changeAll];
        [thread1 cancel];
    }
   
}
-(void)changeAll
{
    if (button1&&button2&button3&button4) {
        [b5 setTitle:@"停止全部" forState:UIControlStateNormal] ;
        button5=YES;
    }
    if (!button1&&!button2&!button3&!button4) {
        [b5 setTitle:@"启动全部" 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:@"停止2" forState:UIControlStateNormal] ;
        [self changeAll];
        [thread2 start];
    }
    else {
        [b2 setTitle:@"启动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:@"停止3" forState:UIControlStateNormal] ;
        [self changeAll];
        [thread3 start];
    }
    else {
        [b3 setTitle:@"启动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:@"停止4" forState:UIControlStateNormal] ;
        [self changeAll];
        [thread4 start];
    }
    else {
        [b4 setTitle:@"启动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:@"停止全部" forState:UIControlStateNormal] ;
        if (!button1) {
            thread1=[[NSThread alloc]initWithTarget:self selector:@selector(tt1) object:nil]; 
            button1=!button1;
            [b1 setTitle:@"停止1" forState:UIControlStateNormal] ;
            [thread1 start];
        }
        if (!button2) {
            thread2=[[NSThread alloc]initWithTarget:self selector:@selector(tt2) object:nil]; 
            button2=!button2;
            [b2 setTitle:@"停止2" forState:UIControlStateNormal] ;
            [thread2 start];
        }
        if (!button3) {
            thread3=[[NSThread alloc]initWithTarget:self selector:@selector(tt3) object:nil]; 
            button3=!button3;
            [b3 setTitle:@"停止3" forState:UIControlStateNormal] ;
            [thread3 start];
        }
        if (!button4) {
            thread4=[[NSThread alloc]initWithTarget:self selector:@selector(tt4) object:nil]; 
            button4=!button4;
            [b4 setTitle:@"停止4" forState:UIControlStateNormal] ;
            [thread4 start];
        }
    }
    else{
        [b5 setTitle:@"启动全部" forState:UIControlStateNormal] ;
        if (button1) {
            button1=!button1;
            [b1 setTitle:@"启动1" forState:UIControlStateNormal] ;
            [thread1 cancel];
        }
        if (button2) {
            button2=!button2;
            [b2 setTitle:@"启动2" forState:UIControlStateNormal] ;
            [thread2 cancel];
        }
        if (button3) {
            button3=!button3;
            [b3 setTitle:@"启动3" forState:UIControlStateNormal] ;
            [thread3 cancel];
        }
        if (button4) {
            button4=!button4;
            [b4 setTitle:@"启动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文件如下

IOS开发/iphone开发多线程