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







發佈了52 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章