IOS开发(100)之线程初窥
1 前言
本章将介绍苹果为简化多线程而推出的一种新方法,成为Grand Central Dispatch(简称易做图),它提供了一套全新的API,可以将应用程序需要执行的工作拆分成为可分散在多个线程和多个CPU上的更小的块,从而解决了用户体验问题。
2 详述
2.1 模拟好使操作
接下来我们模仿一下这个耗时操作建立一个项目,当点击Start Working的时候会等待10秒然后显示内容,并在控制台输出耗时:
代码实例
ZYViewController.m
[plain] //
// ZYViewController.m
// SlowWorker
//
// Created by zhangyuc on 13-6-7.
// Copyright (c) 2013年 zhangyuc. All rights reserved.
//
#import "ZYViewController.h"
@inte易做图ce ZYViewController ()
@end
@implementation ZYViewController
@synthesize startButton,resultsTextView;
-(NSString *)fechSomethingFromServer{
//让线程休眠1秒
[NSThread sleepForTimeInterval:1];
return @"Hi there";
}
-(NSString *)processData:(NSString *)data{
[NSThread sleepForTimeInterval:2];
//大写转换
return [data uppercaseString];
}
-(NSString *)caculateFirstResult:(NSString *)data{
[NSThread sleepForTimeInterval:3];
//获得长度
return [NSString stringWithFormat:@"Number of chars:%d",[data length]];
}
-(NSString *)caculateSenondResult:(NSString *)data{
[NSThread sleepForTimeInterval:4];
//将“e”替换成“E”
return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[startButton release];
[resultsTextView release];
[super dealloc];
}
- (IBAction)doWorking:(id)sender {
//获得当前时间
NSDate* startTime = [NSDate date];
NSString* fetchedData = [self fechSomethingFromServer];
NSString* processedData = [self processData:fetchedData];
NSString* firstResult = [self caculateFirstResult:processedData];
NSString* secondResult = [self caculateSenondResult:processedData];
NSString* resultsSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,secondResult];
//为resultsTextView的text属性赋值
resultsTextView.text = resultsSummary;
NSDate* endTime = [NSDate date];
//获得时间差单位 s
NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
}
@end
//
// ZYViewController.m
// SlowWorker
//
// Created by zhangyuc on 13-6-7.
// Copyright (c) 2013年 zhangyuc. All rights reserved.
//
#import "ZYViewController.h"
@inte易做图ce ZYViewController ()
@end
@implementation ZYViewController
@synthesize startButton,resultsTextView;
-(NSString *)fechSomethingFromServer{
//让线程休眠1秒
[NSThread sleepForTimeInterval:1];
return @"Hi there";
}
-(NSString *)processData:(NSString *)data{
[NSThread sleepForTimeInterval:2];
//大写转换
return [data uppercaseString];
}
-(NSString *)caculateFirstResult:(NSString *)data{
[NSThread sleepForTimeInterval:3];
//获得长度
return [NSString stringWithFormat:@"Number of chars:%d",[data length]];
}
-(NSString *)caculateSenondResult:(NSString *)data{
[NSThread sleepForTimeInterval:4];
//将“e”替换成“E”
return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[startButton release];
[resultsTextView release];
[super dealloc];
}
- (IBAction)doWorking:(id)sender {
//获得当前时间
NSDate* startTime = [NSDate date];
NSString* fetchedData = [self fechSomethingFromServer];
NSString* processedData = [self processData:fetchedData];
NSString* firstResult = [self caculateFirstResult:processedData];
NSString* secondResult = [self caculateSenondResult:processedData];
NSString* resultsSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,secondResult];
//为resultsTextView的text属性赋值
resultsTextView.text = resultsSummary;
NSDate* endTime = [NSDate date];
//获得时间差单位 s
NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
}
@end
运行结果:
初始化:
当点击Start Working后等待大约10秒钟:
控制台运行结果:
2013-06-07 11:18:08.360 SlowWorker[868:c07] Completed in 10.005586 seconds
2.2 线程基础知识
大部分现代操作系统都支持线程的概念。每个进程可以包含多个线程,他们全部同时进行。一个进程中的所有线程共享的可执行程序代码和相同的全局数据。每个线程也可以拥有一些独有的数据。线程可以使用一种称为互斥或者锁的特殊结构,这种结构可以确保特定的代码块无法一次被多个线程运行。有助于保证正确的结果。
再出理线程的时候,常见问题就是线程安全。举例来说在Cocoa Touch中,Foundation框架通常被视为是线程安全的,而UIKit框架在很大程度上被视为不安全的。在运行IOS应用程序中,处理任何的UIKit对象的所有方法调用都应从系统的线程内执行,该线程通常成为主线程。默认情况下,主线程执行IOS应用程序的所有操作(比如处理由用户触发的操作)。
2.3 工作单元
对于多线程操作苹果公司推荐使用的解决方案:将长期运行的任务拆分成多个工作单元,并将这些单元添加到执行队列中。系统会为我们管理这
补充:移动开发 , IOS ,