IOS中多线程应用实践

IOS中多线程应用实践

本文转载至:http://blog.sina.com.cn/s/blog_4c925dca01012e16.html

 

到多线程的应用,应该也不算是什么新鲜的话题了。应该说在很多的开发语言中,都会用到。象java,.net,php,perl等语言中,我们都会看到,用到。当然,在ios的开发语言oc中,也同样如此。但是,没有做过ios开发,或不熟悉oc语言的人来说,也未必知道在oc中如何应用多线程的,有感如此,网络时空(阿堂)在应用一demo后,特此分享了,希望以后用oc来开发ios的多线程应用中的朋友可以参考一下,阿堂就感到很欣慰了!

下面,我们先来看一下demo的运行示意图,再来看看其中代码的具体应用!

 

IOS开发系列之阿堂教程:IOS中多线程应用实践

IOS开发系列之阿堂教程:IOS中多线程应用实践

IOS开发系列之阿堂教程:IOS中多线程应用实践

IOS开发系列之阿堂教程:IOS中多线程应用实践

IOS开发系列之阿堂教程:IOS中多线程应用实践

每个子线程的实现的基本功能如下

1.每个子线程会将相应的计数器递增10次,更次iphone上的相应显示.

2.每次递增后,进入休眠0.5秒。

3.10次循环结束后,会更新总线程的计数器。

4.当用户指示杀掉这个独立线程,或者,杀掉所有线程 (通过点击 Stop All Counting),才会中止循环。

补充一点说明:

本demo中,即使点击停止当前计数操作了,如当前循环在还没有执行完10次计数,也不会停止的,只在当前10次循环结束了,才会真正停止.。。如需马上停止,还需要特殊处理一下,这里阿堂就不再说明了,有兴趣的朋友,可以稍加修改一下内容就可以了,因为本文,阿堂主要是介绍多线程在ios应用开发中一般的实际运用,不想说得太复杂了!

 

ThreadingViewController.h
#import <UIKit/UIKit.h>

@interface ThreadingViewController : UIViewController {

bool button1On;// keeps track if the Start Counting.. buttons are clicked
bool button2On;
bool button3On;
bool button4On;

int total; // keeps track of the total count
int countThread1; //keeps track of the count for each thread
int countThread2;
int countThread3;
int countThread4;

NSLock *myLock; //mutex used to create our Critical Section

IBOutlet UILabel *thread1Label; //Thread value labels
IBOutlet UILabel *thread2Label;
IBOutlet UILabel *thread3Label;
IBOutlet UILabel *thread4Label;

IBOutlet UILabel *totalCount; //Total thread count label
IBOutlet UILabel *updatedByThread; //Updated by thread label

IBOutlet UIButton *button1Title; // buttons titles that will be updated when
//clicked
IBOutlet UIButton *button2Title;
IBOutlet UIButton *button3Title;
IBOutlet UIButton *button4Title;
}

@property (retain,nonatomic) UIButton *button1Title; //getter and setters
@property (retain,nonatomic) UIButton *button2Title;
@property (retain,nonatomic) UIButton *button3Title;
@property (retain,nonatomic) UIButton *button4Title;

@property (retain,nonatomic) UILabel *totalCount; //getter and setters
@property (retain,nonatomic) UILabel *thread1Label;
@property (retain,nonatomic) UILabel *thread2Label;
@property (retain,nonatomic) UILabel *thread3Label;
@property (retain,nonatomic) UILabel *thread4Label;
@property (retain,nonatomic) UILabel *updatedByThread;

-(IBAction)launchThread1:(id)sender; //methods each button will trigger when
//clicked
-(IBAction)launchThread2:(id)sender;
-(IBAction)launchThread3:(id)sender;
-(IBAction)launchThread4:(id)sender;
-(IBAction)KillAllThreads:(id)sender;

 

-(void)thread1;
-(void)thread2;
-(void)thread3;
-(void)thread4;

-(void)displayThread1Counts:(NSNumber*)threadNumber;
-(void)displayThread2Counts:(NSNumber*)threadNumber;
-(void)displayThread3Counts:(NSNumber*)threadNumber;
-(void)displayThread4Counts:(NSNumber*)threadNumber;

-(void)countThreadLoops:(NSNumber*)threadNumber;

@end

 

 

ThreadingViewController.m

#import “ThreadingViewController.h”

@implementation ThreadingViewController
@synthesize totalCount;
@synthesize thread1Label;
@synthesize thread2Label;
@synthesize thread3Label;
@synthesize thread4Label;
@synthesize button1Title;
@synthesize button2Title;
@synthesize button3Title;
@synthesize button4Title;
@synthesize updatedByThread;

 

 

 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad {

button1On = FALSE;
button2On = FALSE;
button3On = FALSE;
button4On = FALSE;
countThread1 = 0;
countThread2 = 0;
countThread3 = 0;
countThread4 = 0;
myLock = [[NSLock alloc]init];
[super viewDidLoad];
}

 

 

-(IBAction)launchThread1:(id)sender
{

if (!button1On)
{

button1On = TRUE;
[button1Title setTitle:@”Kill Counting 1″ forState:UIControlStateNormal];

//工作线程
[self performSelectorInBackground:@selector(thread1) withObject:nil];
}
else
{

button1On = FALSE;
[button1Title setTitle:@”Start Counting 1″ forState:UIControlStateNormal];

}
}

 

-(void)thread1
{

//启动一个线程时,实际上会脱离Cocoa框架。此时,需要我们来负责清理内存池,否则会出现内存泄露
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];// we are responsible for the memory pool
NSNumber *myNumber = [NSNumber numberWithInteger:1]; // the thread number for updating the display

while(button1On)
{

for (int x=0; x<10; x++)
{

[self performSelectorOnMainThread:@selector(displayThread1Counts:) //call the displayThread1Counts method
withObject:myNumber //can be used to to display thread number
waitUntilDone:YES]; //wait until the method returns
[NSThread sleepForTimeInterval:0.5]; //slow things down and sleep for 1/2 second
}
[self performSelectorOnMainThread:@selector(countThreadLoops:) //after 10 increments update the total count
withObject:myNumber //pass the thread number that did the updating
waitUntilDone:NO]; //don’t need to wait. We have a critical section
}

[apool release];

}

 

-(void)displayThread1Counts:(NSNumber*)threadNumber
{

countThread1 += 1;
[thread1Label setText:[NSString stringWithFormat:@”%i”, countThread1]];

}

 

-(IBAction)launchThread2:(id)sender
{

if (!button2On)
{

button2On = TRUE;
[button2Title setTitle:@”Kill Counting 2″ forState:UIControlStateNormal];
[self performSelectorInBackground:@selector(thread2) withObject:nil];
}
else
{

button2On = FALSE;
[button2Title setTitle:@”Start Counting 2″ forState:UIControlStateNormal];

}
}

 

-(void)thread2
{

NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
NSNumber *myNumber = [NSNumber numberWithInteger:2];

while(button2On)
{

for (int x=0; x<10; x++)
{

[self performSelectorOnMainThread:@selector(displayThread2Counts:)
withObject:myNumber
waitUntilDone:YES];
[NSThread sleepForTimeInterval:.5];
}
[self performSelectorOnMainThread:@selector(countThreadLoops:)
withObject:myNumber
waitUntilDone:NO];
}

[apool release];

}

 

-(void)displayThread2Counts:(NSNumber*)threadNumber
{

countThread2 += 1;
[thread2Label setText:[NSString stringWithFormat:@”%i”, countThread2]];

}

-(IBAction)launchThread3:(id)sender
{

if (!button3On)
{

button3On = TRUE;
[button3Title setTitle:@”Kill Counting 3″ forState:UIControlStateNormal];
[self performSelectorInBackground:@selector(thread3) withObject:nil];
}
else
{

button3On = FALSE;
[button3Title setTitle:@”Start Counting 3″ forState:UIControlStateNormal];

}
}

 

-(void)thread3
{

NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
NSNumber *myNumber = [NSNumber numberWithInteger:3];

while(button3On)
{

for (int x=0; x<10; x++)
{

[self performSelectorOnMainThread:@selector(displayThread3Counts:)
withObject:myNumber
waitUntilDone:YES];
[NSThread sleepForTimeInterval:.5];
}
[self performSelectorOnMainThread:@selector(countThreadLoops:)
withObject:myNumber
waitUntilDone:NO];
}

[apool release];

}

 

 

-(void)displayThread3Counts:(NSNumber*)threadNumber
{

countThread3 += 1;
[thread3Label setText:[NSString stringWithFormat:@”%i”, countThread3]];

}

-(IBAction)launchThread4:(id)sender
{

if (!button4On)
{

button4On = TRUE;
[button4Title setTitle:@”Kill Counting 4″ forState:UIControlStateNormal];
[self performSelectorInBackground:@selector(thread4) withObject:nil];
}
else
{

button4On = FALSE;
[button4Title setTitle:@”Start Counting 4″ forState:UIControlStateNormal];

}
}

 

-(void)thread4
{

NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
NSNumber *myNumber = [NSNumber numberWithInteger:4];

while(button4On)
{

for (int x=0; x<10; x++)
{

[self performSelectorOnMainThread:@selector(displayThread4Counts:)
withObject:myNumber
waitUntilDone:YES];
[NSThread sleepForTimeInterval:.5];
}
[self performSelectorOnMainThread:@selector(countThreadLoops:)
withObject:myNumber
waitUntilDone:NO];
}

[apool release];

}

 

-(void)displayThread4Counts:(NSNumber*)threadNumber
{

countThread4 += 1;
[thread4Label setText:[NSString stringWithFormat:@”%i”, countThread4]];

}

 

-(void)countThreadLoops:(NSNumber*)threadNumber
{

//保护临界区的互斥锁
[myLock lock]; //mutex to protect critical section
total += 10;
[self.totalCount setText:[NSString stringWithFormat:@”%i”, total]];
[self.updatedByThread setText:[NSString stringWithFormat:@”Last updated by thread # %i”,[threadNumber integerValue]]];

//确保完成unLock,否则会创建一个死锁
[myLock unlock]; //make sure you perform unlock or you will create a deadlock condition
}

-(IBAction)KillAllThreads:(id)sender
{

button1On = FALSE;
button2On = FALSE;
button3On = FALSE;
button4On = FALSE;

[button1Title setTitle:@”Start Counting 1″ forState:UIControlStateNormal];
[button2Title setTitle:@”Start Counting 2″ forState:UIControlStateNormal];
[button3Title setTitle:@”Start Counting 3″ forState:UIControlStateNormal];
[button4Title setTitle:@”Start Counting 4″ forState:UIControlStateNormal];
}

– (void)dealloc {

[totalCount release];
[thread1Label release];
[thread2Label release];
[thread3Label release];
[thread4Label release];
[button1Title release];
[button2Title release];
[button3Title release];
[button4Title release];
[updatedByThread release];
[myLock release];
[super dealloc];
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110114.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 安全帽识别的原理和系统应用

    安全帽识别的原理和系统应用安全帽识别的原理是用AI技术对工作现场的视频进行实时分析,如果发现工作人员未按要求佩戴安全帽或违规吸烟,系统会自动发出警报,在提醒监理人员的同时,系统会自动保存时间、地点及相应的照片,作为处罚的依据,AI大潮之下,传统的建筑工地也有许多方面得到了提升,除了人脸识别之外,智慧工地最关心的就是安全问题,如何将AI技术应用于安全方面呢?鹰眸安全帽识别系统就是这些特殊区域的守护者。可以说,安全帽智能识别是…

  • anycast技术「建议收藏」

    anycast技术「建议收藏」转载别人的,不好意思啊浅析AnyCast网络技术什么是BGPAnyCast?BGPanycast就是利用一个(多个)as号码在不同的地区广播相同的一个ip段。利用bgp的寻路原则,短的aspath会选成最优路径(bgp寻路原则之n),从而优化了访问速度。其实bgpanycast是不同服务器用了相同的ip地址。阿里的DNS就是使用了BGPAn…

  • 美国城市地名简称_外国地名英文

    美国城市地名简称_外国地名英文近期在做某个项目要用到美国的地名,上网查了一圈都没有比較具体的、专业的,仅仅好自己复制了一个大概有500多个城市、城镇的英文,用谷歌翻译一下,结果例如以下:谷歌翻译结果,非常多是错误的,边用边改美国地

  • 【已解决】Pycharm安装cv2时显示No information available

    【已解决】Pycharm安装cv2时显示No information available【已解决】Pycharm安装cv2时显示Noinformationavailable以下为解决步骤:1、安装opencv-python、numpy、matplotlib;2、然后将pycharm包安装路径下的\venv\Lib\site-packages\cv2下的cv2.cp37-win_amd64.pyd复制到python安装路径下的\venv\Lib\site-packages下;3、验证安装是否成功:输入importcv2,如果不出错代表导入成功。…

  • Python sum() TypeError: ‘int‘ object is not callable xxxxxxxxx XXXXXXXXXX

    Python sum() TypeError: ‘int‘ object is not callable xxxxxxxxx XXXXXXXXXXPythonsum()TypeError:’int’objectisnotcallablexxxxxxxxxXXXXXXXXXX代码中定义了sum变量,导致sum()方法异常。

  • 大话无线通讯基础之:WIFI和5G信道划分

    大话无线通讯基础之:WIFI和5G信道划分目前主流的无线WIFI网络设备802.11a/b/g/n/ac:各种协议的数据传输率:80211.a协议支持的数据传输率:6、9、12、18、24、36、48和54Mbps80211.b协议支持的数据传输率:1、2、5.5和11Mbps80211.g协议支持的数据传输率:6、9、12、18、24、36、48和54Mbps;可以降级到1、2、5.5…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号