进程:包含了某些资源的内存区域,操作系统利用进程把它的分成一些功能单元,进程是活动的程序,是一个容器,是系统资源管理的最小单位。
进程中所包含的一个(单线程)或者多个(多线程)执行单元是线程,进程还有一个私有的虚拟地址空间,该空间仅被它包含的线程访问
线程:线程是进程的一个实体,是CPU调度和分配的基本单位,他是比进程更小的能运行的基本单位,线程自己基本不拥有系统资源,只拥有一点在运行是必须的资源(例如程序计数器,一组寄存器和栈),但是他和在同一进程的其他线程共享进程的所有资源,
线程是在进程容器中运行,实际工作的代码,是程序执行的最小单位,切换代价小
一个程序是由一个或多个相互协作的进程组成的
线程只能归属于一个进程,并且也只能访问该进程的所拥有的资源,当操作系统创建一个进程的时候,该进程会主动申请一个叫做主线程或者首要线程的线程,主线程将会执行运行时宿主,运行时宿主会负责载入CLR
(CLR 公共语言运行时 common language runtime 和虚拟机一样,也是一种运行环境,是一个可以用多种汇编语言使用的运行环境,
CLR的主要功能是内存管理、处理异常、程序集加载、安全性、线程同步,可由面向CLR的所有语言使用,并保证应用和操作系统之间必要的分离)
进程和线程的区别
一个程序至少一个进程,一个进程至少一个线程,线程的划分尺度小于进程,使得多线程程序的并发型高
进程在执行时拥有独立的运行单元,但是线程共享内存单元,极大的提高了运行的效率
进程和线程的主要区别还有就是进程和线程是不同的操作系统资源管理方式,进程拥有独立的地址空间,一个进程崩溃后,在保护模式下其他的进程不会受到影响,而线程只是进程中的不同的执行路径,线程有自己的堆栈和和局部变量,但是线程之间没有独立的内存空间,一个线程死掉就相当于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但是在进程切换的时候,耗费的资源大,效率相对较低,但是对于一些要求同时进行并且又有某些共享变量的时候,只能用线程
1 首先创建一个空的工程 然后创建两个类 在Application文件中,初始化两个类,并且初始化一个UITabBarController 将两个类添加到UITabBarController的viewControllers的属性
代码如下
liFirstViewController *firstViewController = [[liFirstViewControlleralloc]initWithNibName:nilbundle:nil];
liSecondViewController *secondViewController = [[liSecondViewControlleralloc]initWithNibName:nilbundle:nil];
NSArray *viewControllerArray = [NSArrayarrayWithObjects:firstViewController,secondViewController,nil];
UITabBarController *tabBarController = [[UITabBarControlleralloc]init];
tabBarController.viewControllers = viewControllerArray;
//让tabBarViewController作为根视图
self.window.rootViewController = tabBarController;
在第一个类的nib文件中添加几个label 并在相关的文件中关联他的属性
#import <UIKit/UIKit.h>
@interface liFirstViewController :UIViewController
//添加一个进度条
@property (retain,nonatomic) IBOutletUIProgressView *progressView;
//添加一个label用来显示进度条的当前值
@property (retain,nonatomic) IBOutletUILabel *progressResult;
//添加一个button并且关联一个动作
@property (retain,nonatomic) IBOutletUIButton *startButton;
//按钮button的关联动作改变进度条的值 并将其设置为label的text属性
- (IBAction)buttonPressed:(id)sender;
@end
#import "liFirstViewController.h"
@interface liFirstViewController ()
@end
@implementation liFirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.title =@"thread_1";
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
//把进度条当前的值赋值给progressResult
self.progressResult.text = [NSStringstringWithFormat:@"%.2f",self.progressView.progress] ;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//self.progressView.progress = 0;
}
- (void)dealloc {
[_progressResultrelease];
[_progressViewrelease];
[_startButton release];
[superdealloc];
}
- (IBAction)buttonPressed:(id)sender
{
//使按钮按下去之后隐藏
self.startButton.hidden =YES;
//创建一个自动执行的线程出来
[NSThreaddetachNewThreadSelector:@selector(startTheBackGroundJob:)toTarget:selfwithObject:nil];
}
//新建线程要完成的任务
- (void)startTheBackGroundJob:(id)sender
{
//让主线程帮助完成界面的更新
[selfperformSelectorOnMainThread:@selector(updateProgressView:)withObject:nilwaitUntilDone:NO];
}
//叫主线程帮助完成的任务改变进度条的值
- (void)updateProgressView:(id)sender
{
//使当前进度条加0.1
self.progressView.progress +=0.1;
//初始化一个临时变量获取当前进度条的值
float value = self.progressView.progress;
//递归调用
if(value < 1)
{
[NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(updateProgressView:)userInfo:nilrepeats:NO];
}
else
{
self.startButton.hidden =NO;
}
//更新progressResult显示的值
self.progressResult.text = [NSStringstringWithFormat:@"%.2f",self.progressView.progress];
}
@end
在第二个类中
#import <UIKit/UIKit.h>
@interface liSecondViewController : UIViewController
{
int _labelLeftTickets;
int _labelSaleTickes;
NSThread *_firstThread;
NSThread *_secondThread;
//线程状态记录
NSCondition *_ticktesCondition;
}
//初始几个label
@property (retain, nonatomic) IBOutlet UILabel *leftTickets;
@property (retain, nonatomic) IBOutlet UILabel *saleTickes;
@property (retain, nonatomic) IBOutlet UILabel *currentTickets;
@end
#import "liSecondViewController.h"
@interface liSecondViewController ()
@end
@implementation liSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.title = @"thread_2";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化自定义的属性
_labelLeftTickets= 50;
_labelSaleTickes = 0;
_ticktesCondition = [[NSCondition alloc]init];
//添加一个button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 340, 80, 40);
[button setTitle:@"start" forState:UIControlStateNormal];
[button addTarget:self action:@selector(threadStart:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//按钮关联的方法
- (void)threadStart:(id)sender
{
//按钮关联的动作,初始化两个线程 并且关联一个逻辑方法 逻辑处理会涉及到更新界面
_firstThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:nil];
[_firstThread setName:@"thread_One"];
//因为是alloc init方法创建的线程,所以要手动开启
[_firstThread start];
//按钮关联的动作,初始化两个线程
_secondThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:nil];
[_secondThread setName:@"thread_Two"];
[_secondThread start];
}
//逻辑处理的方法 使两个窗口交替操作
- (void)run:(id)sender
{
//循环执行
while (TRUE)
{
[_ticktesCondition lock];
if (_labelLeftTickets > 0)
{
//使进程休眠0.1秒 每0.1秒执行一次
[NSThread sleepForTimeInterval:0.1];
_labelLeftTickets--;
_labelSaleTickes = 50 - _labelLeftTickets;
//获取一下当前线程的名字
NSString *pStr = [[NSThread currentThread]name];
NSLog(@"_labelLeftTickets:%i,_labelSaleTickes:%i,currentThread:%@",_labelLeftTickets,_labelSaleTickes,pStr);
}else if(_labelLeftTickets == 0)
{
NSLog(@"票已经售完");
break;
}
[self performSelectorOnMainThread:@selector(updateMyView:) withObject:[[NSThread currentThread]name] waitUntilDone:NO];
[_ticktesCondition unlock];
}
}
// 更新界面的方法
- (void)updateMyView:(id)sender
{
self.leftTickets.text = [NSString stringWithFormat:@"%i",_labelLeftTickets];
self.saleTickes.text = [NSString stringWithFormat:@"%i",_labelSaleTickes];
self.currentTickets.text = (NSString *)sender;
if (_labelLeftTickets == 0)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"通知" message:@"今日票已经售完" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
[alertView show];
[alertView release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_ticktesCondition release];
[_leftTickets release];
[_saleTickes release];
[_currentTickets release];
[super dealloc];
}
@end