iOS-UI-多线程

多线程:

多线程是为了同步完成多项任务,不是为了提高运行效率,而是为了提高资源使用效率来提高系统的效率。线程是在同一时间需要完成多项任务的时候实现的.

       多线程包括:GCD   NSThread   NSOperation   NSOperation是在GCD语言的基础上开发的,GCD类C语言,NSThread   NSOperation OC语法

iOS允许用户自己开辟新的线程,相对于主线程来说,这些线程称作子线程,子线程和主线程都是独立的运行单元,各自的执行不影响,因此能够并发执行.


实例

首先创建一个button

- (void)button

{


    self.button1 = [[UIButtonalloc] initWithFrame:CGRectMake(50,50, 50, 50)];

    [_button1setTitle:@"点击"forState:UIControlStateNormal];

    [_button1setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    [_button1setTitleColor:[UIColorredColor] forState:UIControlStateHighlighted];

    [self.viewaddSubview:_button1];

    [_button1 release];

}


        button的点击事件

- (void)buttonActions:(id)sender

{

// 每一个跑多线程的地方都加释放池

    @autoreleasepool {

        int k = 0;

        for (int i =1; i < 63; i++) {

            k += i;

            NSLog(@"%d", k);

        }


    }

    //NSObect,NSThread,NSOperation返回主线程方式

    [selfperformSelectorOnMainThread:@selector(createImage:)withObject:@"asdf"waitUntilDone:YES];

    

    

}


- (IBAction)buttonAction:(id)sender {

#warning NSThread

    

//方法中object是往形参上传值的

//    轻量级多线程

//创建NSThread

        NSThread * thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(buttonActions:)object:nil];

        [thread start];

        [thread release];

//第二种创建方法

        [NSThreaddetachNewThreadSelector:@selector(buttonActions:)toTarget:selfwithObject:nil];


}

- (IBAction)buttonAction:(id)sender {

#warning NSObject

  //创建方法

 [self performSelectorInBackground:@selector(buttonActions:)    withObject:nil];

}


- (IBAction)buttonAction:(id)sender {

NSOperationQueue操作队列

NsOperation是抽象的类,所以不能直接使用这个类,而是要使用这个类的子类

    //首先创建一个继承与NsOperation的类NsOperationOne

    //创建NsOperationOne的对象


    NsOperationOne * one = [[NsOperationOnealloc] init];

    //NSOperationQueue 会自动调用NsOperationOnestart方法,所以不写

    //创建NSOperationQueue的对象

    NSOperationQueue * que = [[NSOperationQueuealloc] init];

//  设置NSOperationQueue的最大并发线程个数

    [que setMaxConcurrentOperationCount:1];

    [queaddOperation:one];

    

    [onerelease];

    //线程并发,顺序不清,互不干扰

    //iOS同时运行的多线程不超过10,需要要限制,最大并发数为1的时候线程与线程之间是同步的(等一个线程做完以后再执行下一个)

    //线程同步serial线程并发concurrent

    NsOperationOne * second = [[NsOperationOnealloc]init];

    [queaddOperation:second];

    [secondrelease];

     [que release];

 // NSBlockOperation NSInvocationOperation是NsOperation的子类

 //block里面的内容就是多线程所要执行的代码

    NSBlockOperation * block = [NSBlockOperation blockOperationWithBlock:^{

        

        @autoreleasepool {

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

                NSLog(@"%d", i);

            }

        }

       

    }];

  NSInvocationOperation * invo = [[NSInvocationOperation      alloc]initWithTarget:selfselector:@selector(buttonActions:)         object:nil];

[queueaddOperation:invo];

    [invorelease];

    [queuerelease];


#pragma mark GCD多线程

//同步线程队列

- (void)createSerialGCD

{

    //第一步:创建一个同步线程队列

    //C语言第一个参数 队列的名字第二个参数是标记是同步队列

    dispatch_queue_t queue =dispatch_queue_create("first",DISPATCH_QUEUE_SERIAL);

    //第二步:异步执行同步线程队列(异步是与主线程异步)

   dispatch_async(queue, ^{

        //多线程的代码, GCD中不能写自动释放池(因为GCDC语言)

        //子线程不能修改UI界面

        

        //下载一张图片,并显示到主界面(下载用同步下载)

        NSString * urlStr =@"http://img.hb.aicdn.com/258541cf3d0b5dc5c68f9f3096a830b68288d2011f65f-00EuvV_fw658";

        //网址

       NSURL * url = [NSURLURLWithString:urlStr];

        

        NSMutableURLRequest * req = [NSMutableURLRequestrequestWithURL:url];

        [reqsetHTTPMethod:@"GET"];

       NSURLResponse * response = nil;

       NSError * error = nil;

       NSData * data = [NSURLConnectionsendSynchronousRequest:req returningResponse:&response error:&error];

       UIImage * image = [UIImageimageWithData:data];

        

        //显示到界面上,所有跟UI有关的内容都要到主线程运行

       //返回主线程

        dispatch_async(dispatch_get_main_queue(), ^{

           //在主线程运行

           UIImageView * imageV = [[UIImageViewalloc] initWithFrame:CGRectMake(250,350, 100, 150)];

            imageV.image = image;

            [self.viewaddSubview:imageV];

            [imageVrelease];

            

        });

    });

}


//*******

//并行队列

- (void)createConnectGCD

{

    //第一个参数:级别

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

       

        NSURL * url = [NSURLURLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/241f95cad1c8a786beb13e066509c93d70cf501a.jpg"];

//数据

       NSData * data = [NSDatadataWithContentsOfURL:url];

//照片

       UIImage * image = [UIImageimageWithData:data];

//返回主线程

        dispatch_async(dispatch_get_main_queue(), ^{

           UIImageView * imageV = [[UIImageViewalloc] initWithFrame:CGRectMake(50,100, 100, 150)];

            imageV.image = image;

            [self.viewaddSubview:imageV];

            [imageVrelease];

        });

       

    });

}







 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值