iOS 之多线程用法示例

//在.h文件中自定义属性button1,imageView1,button2,button3

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, retain)UIButton *button1;

@property (nonatomic, retain)UIImageView *imageView1;

@property (nonatomic, retain)UIButton *button2;

@property (nonatomic, retain)UIButton *button3;

@end

//在.m文件中

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor greenColor];

    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    self.button1.frame = CGRectMake(40, 160, 100, 100);

    self.button1.backgroundColor = [UIColor redColor];

    [self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.button1];

    self.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(40, 300, 100, 100)];

    self.imageView1.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:self.imageView1];

    [_imageView1 release];

    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];

    self.button2.frame = CGRectMake(200, 160, 100, 100);

    self.button2.backgroundColor = [UIColor purpleColor];

    [self.button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.button2];

    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];

    self.button3.frame = CGRectMake(200, 300, 100, 100);

    self.button3.backgroundColor = [UIColor blackColor];

    [self.button3 addTarget:self action:@selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.button3];  

}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

#pragma mark -- 多线程之NSThread

- (void)button1Action:(UIButton *)button1

{

       NSLog(@"NSThread");

    //自定义多线程

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadAction:) object:@"大水杯"];//object在最后一般为传参数

    //开启多线程

    [thread start];

    //使用系统的

    [NSThread detachNewThreadSelector:@selector(systemThreadDownloadImage) toTarget:self withObject:nil];

     NSLog(@"[NSThread currentThread]===== %@ [NSThread isMainThread] ===== %d", [NSThread currentThread], [NSThread isMainThread]);

    [thread release];

}

- (void)threadAction:(NSString *)str

{

    //打印当前线程, 以及是否为主线程-

    NSLog(@"[NSThread currentThread]===== %@ [NSThread isMainThread] ===== %d", [NSThread currentThread], [NSThread isMainThread]);

    NSLog(@"str === %@", str);

//这种循环必须写在子线程中,否则程序会被卡死

//    for (int i = 0; i < 1000000; i++) {

//        NSLog(@"i == %d", i);

//    }


}

- (void)systemThreadDownloadImage

{

    NSString *urlStr = @"http://amuse.nen.com.cn/imagelist/11/21/9as70n3ir61b.jpg";

    NSURL *url = [NSURL URLWithString:urlStr];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImage *image = [UIImage imageWithData:data];

    self.imageView1.image = image;

}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

- (void)button2Action:(UIButton *)button2

{

    NSLog(@"NSOperation");

    //NSOperation  是抽象类  我们用她提供的两个子类

    //NSInvocationOperation   创建一个对象就是一个任务

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(NSOperationAction:) object:@"登山包"];

    //block内执行的方法相当于子线程执行的方法

    //NSBlockOperation   创建一个对象就是一个任务

    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

        for (int i = 0; i < 10; i++)

        {

            NSLog(@"i == %d", i);

        }

     }];

//队列  用来管理队列里的一个或者多个任务

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(NSOperationAction1) object:nil];

    //最大允许并发执行任务的个数

    [queue setMaxConcurrentOperationCount:2];

    //绑定一个任务,就是前者必须等后者执行完成才能执行

    [operation1 addDependency:blockOperation];

    //将任务加到队列中

    [queue addOperation:operation];

    [queue addOperation:operation1];

    [queue addOperation:blockOperation];

    [queue release];

    [operation release];

    [operation1 release];

}

- (void)NSOperationAction:(NSString *)str

{

    NSLog(@"str === %@", str);

}

- (void)NSOperationAction1

{

    NSLog(@"operation1");

}

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//***********************************************************************************

- (void)button3Action:(UIButton *)button3

{

    // GCD的串行

    NSLog(@"GCDserial");

#pragma mark --- GCD之串行

//    //串行特点 一个一个执行, 一个执行完再执行下一个    

//    //1.系统自带串行系列    

//#warning 重点经常用到  作用:从子线程上得到主线程  

//    //1.1创建系统主队列

//    dispatch_queue_t mainQueue = dispatch_get_main_queue(); 

//    //1.2执行主队列

//    dispatch_async(mainQueue, ^{

//        //加入任务  视频   音频

//        NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//    });

//#warning 重要  非常常见的写法   

//    dispatch_async(dispatch_get_main_queue(), ^{    

//        NSLog(@"获得主线程的大众写法");     

//    });

//    //2.自定义串行并列

//    //2.1创建自定义串行队列

//    //参数1.队列标示符 一般是逆向域名

//    //参数2.队列类型

//    dispatch_queue_t mySerialQueue = dispatch_queue_create("com.lanou3g.GCD.Seriel", DISPATCH_QUEUE_SERIAL);

//    //2.2执行队列

//    dispatch_async(mySerialQueue, ^{

//   NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//    });    

#pragma mark --- GCD  并行

#warning 重要 经常跟dispatch_get_main_queue 一起使用

//    //1.得到全局队列

//    //参数1.队列有限级

//    //参数2.苹果预留参数, 默认给0

//    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//    dispatch_async(globalQueue, ^{

//        NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"3 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"4 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"5 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"6 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"7 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//        NSLog(@"8 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//    });

//    //2.自定义并行队列

//    //2.1创建自定义并行队列

//    dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.GCD.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);

//    //2.2执行并行队列

//   dispatch_async(myConcurrentQueue, ^{

//       NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//    });

//    dispatch_async(myConcurrentQueue, ^{

//        NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);

//    });

//#warning 重要

//    //练习  子线程请求图片  主线程负责刷新显示图片

//    //dispatch_get_global_queue 创建系统并行队列

//    //dispatch_async 执行队列

//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//       //此块中是子线程

//        NSString *urlStr = @"http://amuse.nen.com.cn/imagelist/11/21/9as70n3ir61b.jpg";

//        NSURL *url = [NSURL URLWithString:urlStr];

//        NSData *data = [NSData dataWithContentsOfURL:url];

//        UIImage *image = [UIImage imageWithData:data];

//        //在主线程中显示图片

//        __block ViewController *vc = self;

//        dispatch_async(dispatch_get_main_queue(), ^{

//            NSLog(@"获得主线程的大众写法");

//            vc.imageView1.image = image;

//        });

//    });

#warning 重要

    //设置多少秒后才执行

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"空空空");

    });

    //2.只执行一次

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"%@ %d", [NSThread currentThread], [NSThread isMainThread]);

    });

}

//***********************************************************************************

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值