有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的体验天壤之别,所以多线程是一个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.线程池,可以了解下

用多线程实现一个简单的程序

#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开发多线程的更多相关文章

  1. iphone开发 IOS 组织架构图

    转载自 :http://blog.csdn.net/mashi321323/article/details/18267719   登录|注册     mashi321323的专栏       目录视图 ...

  2. iOS开发UI篇—iPad和iPhone开发的比较

    一.iPad简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定位介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2.iPad的市场情况 ...

  3. iOS开发---iPhone SDK 包含哪些东西?

    第一部分: 在使用Intel芯片的Macintosh计算机开发iOS应用程序所需的全部接口.工具以及资源全都包含于iPhone SDK. 苹果公司将大部分系统接口发布在框架这种特殊的数据包.一个框架就 ...

  4. 【汇总】涉及iOS&iPhone开发相关文章汇总

    此文章汇总本博客中有涉及iPhone开发的相关文章,不定时更新中... 1.NSUserDefaults快速存储数据: http://www.cnblogs.com/ios-wmm/archive/2 ...

  5. iOS开发教程之:iPhone开发环境搭建

    安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...

  6. ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)

    原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...

  7. iOS:iPad和iPhone开发的异同(UIPopoverController、UISplitViewController)

    iPad和iPhone开发的异同 1.iPhone和iPad: niPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \ ...

  8. 【转】iOS开发UI篇—iPad和iPhone开发的比较

    原文网址:http://www.cnblogs.com/wendingding/p/3918007.html iOS开发UI篇—iPad和iPhone开发的比较 一.iPad简介 1.什么是iPad ...

  9. iOS开发网络篇—多线程断点下载

    iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了同时开启多条线程下载一个较大的文件.因为实现过程较为复杂,所以下面贴出完整的代码. 实现思路:下载开始, ...

随机推荐

  1. 北京联想招聘-java 云服务开发工程师 加入qq 群:220486180 或者直接在此 留言咨询

     Position Title: 云服务开发工程师 Experience Required:  3 - 5 Years 负责联想企业网盘服务端开发  Position Requirements  1. ...

  2. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  3. LeetCode 笔记26 Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. mysql命令行

    mysql -u root -p create database bookstore; drop database bookstore; use bookstore create table user ...

  5. .NET 关键字

    一.base关键字 可以通过base关键字访问上一级父类方法的访问.静态static函数无法调用base 二.new 关键字new new有2个作用. new运算符   用来分配内存空间和初始化对象. ...

  6. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  7. [C#]MemoryStream.Dispose之后,为什么仍可以ToArray()?

    目录 概述 MemoryStream分析 总结 概述 事件起因,一哥们在群里面贴出了类似下面这样的一段代码: class Program { static void Main(string[] arg ...

  8. [c#基础]堆和栈

    前言 堆与栈对于理解.NET中的内存管理.垃圾回收.错误和异常.调试与日志有很大的帮助.垃圾回收的机制使程序员从复杂的内存管理中解脱出来,虽然绝大多数的C#程序并不需要程序员手动管理内存,但这并不代表 ...

  9. WCF 入门 (21)

    前言 再不写一篇就太监了,哈哈. 第21集 WCF里面的Binding Bindings in WCF 其实不太了解为什么第21集才讲这个Binding,下面都是一些概念性的东西,不过作为一个入门视频 ...

  10. QQ面向对象设计

          通讯项目--仿QQ聊天程序   详细设计说明书                                                         一.引言 此项目为验证Jav ...