- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
//多线程
// 1
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutable1) object:nil];;
[thread start];
[thread release];
// 2
[NSThread detachNewThreadSelector:@selector(mutable1) toTarget: self withObject:nil];
// 3 NSOBject 线程
[self performSelectorInBackground:@selector(mutable1) withObject:nil];
// 4 block 线程
NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[operationQueue addOperationWithBlock:^{
NSLog(@"do some ...");
}];
// 5 添加一个线程 到线程队列, 可以添加多个,有最大线程数,
NSOperationQueue *operationQueue2 = [[NSOperationQueue alloc]init];
//线程并发数
operationQueue2.maxConcurrentOperationCount = 1;
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutable1) object:nil];
[operationQueue2 addOperation:operation];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutable1) object:nil];
[operationQueue2 addOperation:operation2];
return YES;
// 6
}
- (void)mutable1
{
for (int i = 0; i < 10; i ++){
NSLog(@"gggg ");
}
// 多线程执行完之后,会到主线程
// 比如 请求完网络数据后,刷新ui界面(主线程)
[self performSelectorOnMainThread:@selector(mainThred) withObject:nil waitUntilDone:NO];
}
- (void)mainThread
{
for (int i = 0; i < 100; i ++){
NSLog(@"mainThred");
}
}
//GCD
//创建一个队列
dispatch_queue_t queue = dispatch_queue_create("test", nil);
//异步多线程
dispatch_async(queue, ^{
for (int i = 0; i < 100; i ++){
NSLog(@"多线程 thread %i ", i);
}
BOOL isMuliti = [NSThread isMultiThreaded];
if (isMuliti){
NSLog(@"多线程");
}
//执行完成之后,回到主线程
dispatch_sync(dispatch_get_main_queue(), ^{
BOOL isMain = [NSThread isMainThread];
if (isMain){
NSLog(@"主线程");
}
});
});
// 同步运行在当前线程
dispatch_sync(queue, ^{});
for (int i = 0; i < 100; i ++){
NSLog(@"main thread %i ", i);
}
eg异步加载数据
#import <UIKit/UIKit.h>
@interface UIImageView (WebCatch)
- (void)setImageWithURL:(NSURL *)url;
@end
#import "UIImageView+WebCatch.h"
@implementation UIImageView (WebCatch)
- (void)setImageWithURL:(NSURL *)url
{
dispatch_queue_t queue = dispatch_queue_create("loadImage", nil);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//UI的操作主要放在主线程上去处理
dispatch_async(dispatch_get_main_queue(), ^{
self.image = image;
});
});
}
@end
#import "ViewController.h"
#import "UIImageView+WebCatch.h"
@interface ViewController ()
@end
@implementation ViewController
- (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)loadView
{
[super loadView];
for (int i = 0; i < 10; i ++){
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, i * 50, 50, 50)];
[imageview setImageWithURL:[NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/w%3D2048/sign=0ef89c67cf1b9d168ac79d61c7e6b58f/a71ea8d3fd1f4134ef340bc6241f95cad1c85e02.jpg"]];
[self.view addSubview:imageview];
[imageview release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 80, 40);
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)onclick
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"iii" delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil];
[alert show];
[alert release];
}
@end
NSRunLoop
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
// self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
// self.window.rootViewController = self.viewController;
//开启一个线程
[self performSelectorInBackground:@selector(multithread) withObject:nil];
[self.window makeKeyAndVisible];
return YES;
}
- (void)multithread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
//默认添加到了runloop里面
// [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//没有添加到runloop中,需手动添加
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//添加到runloop中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[pool release];
[[NSRunLoop currentRunLoop] run];
NSLog(@"线程over");
}
- (void)timerAction
{
NSLog(@"time out");
}