有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的体验天壤之别,所以多线程是一个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. 《生活就像练习》读书笔记(一)——AQAL理论和象限

    摘自<生活就像练习>肯威尔伯 著 北京:同心出版社,2012.6 AQAL整合理论 AQAL的意思是“所有象限.所有层面.所有路线.所有状态.所有类型”.练习的真正目的是:努力阐释瞬息万变 ...

  2. Android ViewPager使用详解

    这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...

  3. matlab中findstr,strfind,strcmp,strncmp区别与联系

    在Matlab中,这几个函数区分如下: (以下默认S1和S2是字符串,同样也适用于cell细胞类型数据,也就是循环对cell中每个元素分别判断即可.) findstr(S1,S2):寻找是否有S1和S ...

  4. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  5. double相加(減)结果会有些误差

    前提介绍 今天在调试代码的时候发现了一个double类型数据相减的有趣问题,148163.1 - 82692.09大家猜猜结果等于多少,经过调试最终为5471.010000000009. 是不是很奇怪 ...

  6. Javascript设计模式之我见:迭代器模式

    大家好!本文介绍迭代器模式及其在Javascript中的应用. 模式介绍 定义 提供一种方法顺序一个聚合对象中各个元素,而又不暴露该对象内部表示. 类图及说明 Iterator抽象迭代器 抽象迭代器负 ...

  7. 关于JavaScript打印去掉页眉页脚

    因为这个问题,Google和百度都查了个遍,网上主要解决方案都是这一个代码: <script language="JavaScript"> var hkey_root, ...

  8. 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来

    代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ...

  9. ubuntu中管理用户和用户组

    1. 添加一个用户组并指定id为1002 sudo groupadd -g 1002 www 2. 添加一个用户到www组并指定id为1003 sudo useradd wyx -g 1002 -u ...

  10. Node on Mac 初步

    安装node之前 1.之前在windows平台上安装node,直接下载一个安装文件得了.但是mac osx是基于unix架构的嘛,跟linux很像啊. 在linux上安装我们多习惯用命令行工具安装啊, ...