@interface MainViewController : UIViewController{
UIImageView *_aImage;
}
-(void)createOperation{
//同时运行的线程叫并发线程 最大线程数不超过10个
//非协议网络链接都是同步加载 卡主线程
CustomOpe *nsthread = [[CustomOpe alloc]init] ;
//[nsthread start];
NSOperationQueue *nsoperation = [[NSOperationQueue alloc]init];
[nsoperation addOperation:nsthread];
CustomOpe *nsthread1 = [[CustomOpe alloc]init] ;
[nsoperation addOperation:nsthread1];
[nsoperation setMaxConcurrentOperationCount:1];//最大并发数为1是线程的同步
[nsthread release];
[nsthread1 release];
}
//创建子线程
#pragma mark -
#pragma mark NSThread
-(void)createThread{
//当前的方法是否在主线程中运行[进程号:线程号]
if ([NSThread isMainThread]) {
NSLog(@"主线程");
}
// NSThread *nsthread = [[NSThread alloc]initWithTarget:self selector:@selector(createAddNumber:) object:nil];
// [nsthread start];
[NSThread detachNewThreadSelector:@selector(createAddNumber:) toTarget:self withObject:nil];
}
- (void)createAddNumber:(id)sender{
//自动释放池
// 早期的写法
// NSAutoreleasePool *pool = [NSAutoreleasePool alloc];
@autoreleasepool {
//
// for (int i =0; i<6355000; i++) {
//
// NSLog(@"i =%d",i);
// }
// [pool drain];
NSURL *url =[NSURL URLWithString:@"http://img.chengmi.com/cm/021da391-512b-41e7-af65-cfaaecb19654.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *currentImage = [UIImage imageWithData:data];
//把操作ui必须返回主线程 将图片传过去
[self performSelectorOnMainThread:@selector(changeImageview:) withObject:currentImage waitUntilDone:YES];
}
}
-(void)changeImageview:(UIImage *)image{
//把图片
[_aImage setImage:image];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@implementation CustomOpe
- (void)dealloc
{
[super dealloc];
}
-(id)init{
self = [super init];
if (self) {
}
return self;
}
//重写Mian方法 程序调用完init 方法后,立刻执行的该方法,做多线程操作
- (void)main{
@autoreleasepool {
[self ope];
}
}
- (void)ope{
for ( int i =0; i<10; i++) {
NSLog(@"%d",i);
}
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = NO;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 70, 100, 100);
[button setTitle:@"点击" forState: UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted ];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(createConcurrentGCD) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_aImage= [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 200, 100)];
[self.view addSubview:_aImage];
[_aImage release];
}
//创建一个并发的GCD线程队列
-(void)createConcurrentGCD{
//异步执行并发线程队列 参数一 执行级别 参数二 系统的保留参数
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://img.chengmi.com/cm/021da391-512b-41e7-af65-cfaaecb19654.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *currentImage = [UIImage imageWithData:data];
//异步返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
[_aImage setImage:currentImage];
});
});
}
- (void)createGCD{
//创建一个线程队列 参数1 队列名字是first 参数二 选择队列类型 DISPATCH_QUEUE_SERIAL 现在是同步的线程队列
dispatch_queue_t firstQueue = dispatch_queue_create("first", DISPATCH_QUEUE_SERIAL);
//异步执行线程队列
dispatch_async(firstQueue, ^{
//数据库查询 网络请求都可以写
for (int i=0; i<10; i++) {
NSLog(@"i===%d",i);
}
});//这两个叫同步线程队列
// dispatch_async 代表对于主线程来说是异步执行后面的方法
//firstQueue 是针对与它本身来说是一个同步队列
//block 是它本身添加到firstQueue 这同步队列里面执行
dispatch_async(firstQueue, ^{
//数据库查询 网络请求都可以写
for (int i=0; i<10; i++) {
NSLog(@"%d",i);
}
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}