NAThread线程

//  UIImageView+loadNetImage.h

#import <UIKit/UIKit.h>

@interface UIImageView (loadNetImage)

//在网络上加载图片如果加载不成功,就显示默认图片;
- (void)setImageWithURL:(NSString *)urlString andDefaultImage:(NSString *)imageName;

@end

//  UIImageView+loadNetImage.m

#import "UIImageView+loadNetImage.h"

@implementation UIImageView (loadNetImage)

- (void)setImageWithURL:(NSString *)urlString andDefaultImage:(NSString *)imageName {
    
    self.image = [UIImage imageNamed:imageName];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [NSURL URLWithString:urlString];
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:imageData];
        if (image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.image = image;
            });
        }
    });
    
}

@end

<pre name="code" class="objc">//  ViewController.m

#import "ViewController.h"
#import "UIImageView+loadNetImage.h"
#import "Base_Config.h"

@interface ViewController ()

{
   @private int ticket_total;
    NSLock *mylock;
    NSCondition *myCondition;
    NSInvocationOperation  *operation1;
    NSInvocationOperation  *operation2;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    ticket_total = 100;
    mylock = [NSLock new];
    myCondition = [NSCondition new];
    
    
    UIImageView *logo = [[UIImageView alloc]initWithFrame:CGRectMake(80, 400, 150, 80)];
    [logo setImageWithURL:@"http://www.baidu.com/img/bdlogo.png" andDefaultImage:DEFAUT_IMAGE];
    
	[self.view addSubview:logo];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)base_NSThread:(UIButton *)sender {
    //调用start方法才能执行
     //<1.>
    NSThread *thread = [[NSThread  alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
    //设置优先级、线程名字
    [thread setThreadPriority:100];
    [thread setName:@"ios"];
    
    [thread start];
    
     //<2.>
    //自动执行
//    [NSThread detachNewThreadSelector:@selector(threadRun) toTarget:self withObject:nil];
    
}

- (void)threadRun {
    while (true) {
        //获得当前线程的名字
        NSLog(@"thread......%@",[[NSThread currentThread] name]);
    }
}

- (IBAction)image_load:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
    //<1.>
//    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(load_imageWithURL:) object:url];
//    [thread start];
    //<2.>
    [NSThread detachNewThreadSelector:@selector(load_imageWithURL:) toTarget:self withObject:url];
    
}

- (void)load_imageWithURL:(NSURL *)url {
    [NSThread sleepForTimeInterval:2];
    if ([operation1 isCancelled]) {
        NSLog(@"canceled......");
        return;
    }
    NSData *image_data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:image_data];
    //调用主线程的更新方法
    [self performSelectorInBackground:@selector(updateImagewithImage:) withObject:image];
}

- (void)updateImagewithImage:(UIImage *)image  {
    UIImageView *image_view = [[UIImageView alloc]initWithFrame:CGRectMake(80, 170, 150, 80)];
    
    image_view.image = image;
    [self.view addSubview:image_view];
}

- (IBAction)thread_lock:(UIButton *)sender {
    
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(sale_tickets) object:nil];
    thread1.name = @"thread_1";
//    [thread1 setThreadPriority:100];
    [thread1 start];
    
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(sale_tickets) object:nil];
    thread2.name = @"thread_2";
//    [thread2 setThreadPriority:300];
    [thread2 start];
    
    NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(testWait) object:nil];
    thread3.name = @"thread_3";
//    [thread3 setThreadPriority:1];
    [thread3 start];
    
}

- (void)sale_tickets {
    //线程同步,线程并发
    while (true) {
        //加锁
        //[mylock lock];
        [myCondition lock];
        //等通知
        [myCondition wait];
        
        [NSThread sleepForTimeInterval:1];
        NSLog(@"%i",ticket_total);
        NSLog(@"thread_name.......%@",[[NSThread currentThread]name]);
        ticket_total--;
        if (ticket_total < 0) {
            break;
        }
        //解锁
        //[mylock unlock];
        [myCondition unlock];
    }
}

- (void)testWait {
  //  while (true) {
        [myCondition lock];
        NSLog(@"通知....");
        //发送通知
        [myCondition signal];
        
        [NSThread sleepForTimeInterval:2];
        
        [myCondition unlock];
  //  }
    
}

- (IBAction)operation_load:(UIButton *)sender {
    
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
    operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(load_imageWithURL:) object:url];
//    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
//    [mainQueue addOperation:operation1];
    
    operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWait) object:nil];
    //在执行operation1之前执行operation2
    [operation1 addDependency:operation2];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation2];
    [queue addOperation:operation1];
    
    //取消执行
    [NSThread sleepForTimeInterval:1];
    //[operation1 cancel];
    
}

- (IBAction)gcd_load:(UIButton *)sender {
    
    //异步提交(对列,功能block);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 300, 150, 80)];
            imageView.image = image;
            [self.view addSubview:imageView];
        });
    });
    
}

@end







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值