• iOS UI 21 线程


    //

    //  RootViewController.m

    //  Ui - 21 线程

    //

    //  Created by dllo on 15/12/9.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "RootViewController.h"

    #import "MyOperation.h"

    @interface RootViewController ()

    @property (retain, nonatomic) IBOutlet UIImageView *imagev;


    @end


    @implementation RootViewController


    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

    }

    - (void)threadAct

    {

        NSLog(@"当前线程:%@", [NSThread currentThread]);

    //    for (NSInteger i = 0; i < 600000; i++) {

    //        NSLog(@"threadAct:%ld",i);

    //    }

        //休眠

        [NSThread sleepForTimeInterval:1.0];

        NSLog(@"分支线程休眠结束");

        

    }

    - (IBAction)thread:(UIButton *)sender {

        //NSthread系统提供的轻量级线程方式

        // 优点: 创建简单

        // 缺点: 提供了太多的操作需要自己设置,若要使用需要对线程特别了解

        //每次创建一个对象就是一个线程

        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadAct) object:nil];

        //开始执行线程

        [thread start];

        [thread release];

        

        

        NSLog(@":%@", [NSThread mainThread]);

        NSLog(@"判断是否为主线程:%d", [NSThread isMainThread]);

    //    for (NSInteger i = 0; i < 600000; i++) {

    //        NSLog(@"主线程:%ld",i);

    //    }

        [NSThread sleepForTimeInterval:2.0];

        NSLog(@"主线程");

        


    }


    - (void)thAction{

        [NSThread sleepForTimeInterval:1.0];

        NSLog(@"分支线程休眠结束");

    }

    - (IBAction)object:(id)sender {

        

        //NSObject 的线程方式

        //优点:使用简单

        //缺点:没有线程安全方面控制

        

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

        NSLog(@":%@", [NSThread mainThread]);

        NSLog(@"判断是否为主线程:%d", [NSThread isMainThread]);

        [NSThread sleepForTimeInterval:2.0];

        NSLog(@"主线程休眠结束");

        

        

        

    }

    // 管理线程

    - (IBAction)NSOperation:(id)sender {

    //    NSOperation 本身并没有并发的能力, 需要添加进NSOperationQueue队列中才可以

    //    执行NSOperation仅是单独值和ing里面的main方法

        MyOperation *op1 = [[MyOperation alloc]init];

    //    [op1 start];

          MyOperation *op2 = [[MyOperation alloc]init];

          MyOperation *op3 = [[MyOperation alloc]init];

          MyOperation *op4 = [[MyOperation alloc]init];

        // 创建管理队列

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

        

        

        // 设置最大并发数

        //注意,一定要在添加进队列前设置

        

        [queue setMaxConcurrentOperationCount:2];

        //添加进队列

        [queue addOperation:op1];

        [queue addOperation:op2];

        [queue addOperation:op3];

        [queue addOperation:op4];

        

        [op1 release];

        [op2 release];

        [op3 release];

        [op4 release];

       

        

        

    }

    - (IBAction)GCD:(id)sender {

        

        //创建自己的队列

        //参数1 , 标识

        //参数2 , DISPATCH_QUEUE_SERIAL - 串行

        //   DISPATCH_QUEUE_CONCURRENT - 并行

    //   dispatch_queue_t myQ =  dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);

    //    //向队列中添加任务

    //  dispatch_async(myQ, ^{

    //      

    //      [self thAction];

    //  });

    //    dispatch_async(myQ, ^{

    //        

    //        [self thAction];

    //    });

        

        //系统自带5个队列

        //4个并列 , 一个串

        

        //除了自定义的队列外,系统提供了5个队列 - 一个串行队列和4个并行队列

        //1个串行队列:主队列,专门用来管理主线程的

        //4个并行队列:全局队列(单例方式),具有不同的优先级

        

        

        //主队列

        

        dispatch_queue_t mainQueue = dispatch_get_main_queue();

        

        

        //全局队列

        //参数1 优先级

        //参数2 预留参数,只能填0;

        dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        //网络请求

        dispatch_async(globalQueue, ^{

            NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"];

            NSData *Data = [NSData dataWithContentsOfURL:url];

            UIImage *image = [UIImage imageWithData:Data];

            NSLog(@"image请求完毕");

    //        self.imagev.image = image;

            //分支线程对于界面铺键刷新等操作的优先级很低,所以此类操作一般回到主线程进行操作

            

            dispatch_async(mainQueue, ^{

                

                self.imagev.image = image;

            });

        });

        

       static dispatch_once_t  once= 0;

        dispatch_once(&once , ^{

            

            NSLog(@"只执行一次");

        });

    }


    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }


    /*

    #pragma mark - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */


    - (void)dealloc {

        [_imagev release];

        [super dealloc];

    }


    @end

    //

    //  MyOperation.h

    //  Ui - 21 线程

    //

    //  Created by dllo on 15/12/9.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import <Foundation/Foundation.h>


    @interface MyOperation : NSOperation


    @end

    //

    //  MyOperation.m

    //  Ui - 21 线程

    //

    //  Created by dllo on 15/12/9.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "MyOperation.h"


    @implementation MyOperation

    - (void)main

    {

        NSLog(@"===");

        [NSThread sleepForTimeInterval:1.0];

        NSLog(@"睡醒了");

    }

    @end




  • 相关阅读:
    Centos7下部署两套python版本并存
    运维监控系统之Open-Falcon
    Linux下如何查看系统启动时间和运行时间以及安装时间
    linux下使用FreeRDP 连接 Windows 远程桌面
    python3.6环境部署文档
    应用Fiddler对手机应用来抓包
    Highcharts 向下钻取饼图
    Highcharts 散点图
    IntelliJ IDEA2017 激活方法
    iterable 类型
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043066.html
Copyright © 2020-2023  润新知