zoukankan      html  css  js  c++  java
  • 源码0501-04-了解-线程的状态

    04-了解-线程的状态

    //  ViewController.m
    //  04-了解-线程的状态
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
    }
    
    - (void)run
    {
        for (NSInteger i = 0; i<100; i++) {
            NSLog(@"-----%zd", i);
            
            if (i == 49) {
                [NSThread exit]; // 直接退出线程
            }
        }
    }
    
    - (void)run2
    {
        NSLog(@"-------");
    //    [NSThread sleepForTimeInterval:2]; // 让线程睡眠2秒(阻塞2秒)
    //    [NSThread sleepUntilDate:[NSDate distantFuture]];
        [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
        NSLog(@"-------");
    }
    
    @end

    05-掌握-线程安全

    加锁-多个线程同时干一件事件会有问题;

    //  ViewController.m
    //  05-掌握-线程安全
    #import "ViewController.h"
    
    @interface ViewController ()
    /** 售票员01 */
    @property (nonatomic, strong) NSThread *thread01;
    /** 售票员02 */
    @property (nonatomic, strong) NSThread *thread02;
    /** 售票员03 */
    @property (nonatomic, strong) NSThread *thread03;
    
    /** 票的总数 */
    @property (nonatomic, assign) NSInteger ticketCount;
    
    /** 锁对象 */
    //@property (nonatomic, strong) NSObject *locker;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    self.locker = [[NSObject alloc] init];
        
        self.ticketCount = 100;
        //创建了3条线程;给每条线程去了名字;
        self.thread01 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
        self.thread01.name = @"售票员01";
        
        self.thread02 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
        self.thread02.name = @"售票员02";
        
        self.thread03 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
        self.thread03.name = @"售票员03";
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.thread01 start];
        [self.thread02 start];
        [self.thread03 start];
    }
    
    - (void)saleTicket
    {
        while (1) {
            @synchronized(self) {传一个锁的标识对象
                // 先取出总数
                NSInteger count = self.ticketCount;
                if (count > 0) {
                    self.ticketCount = count - 1;
                    NSLog(@"%@卖了一张票,还剩下%zd张", [NSThread currentThread].name, self.ticketCount);
                } else {
                    NSLog(@"票已经卖完了");
                    break;
                }
            }
        }
    }
    
    @end

    06-掌握-线程间通信-下载图片

    //  ViewController.m
    //  06-掌握-线程间通信-下载图片
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // 另外一种线程之间的通信方式
      //    NSPort;
      //    NSMessagePort;
      //    NSMachPort;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       //直接就是self调用了 [self performSelectorInBackground:@selector(download3) withObject:nil]; }
    - (void)download3 { // 图片的网络路径 NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"]; // 加载图片 NSData *data = [NSData dataWithContentsOfURL:url]; // 生成图片 UIImage *image = [UIImage imageWithData:data]; // 回到主线程,显示图片 [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO]; // [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; // [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES]; } //- (void)showImage:(UIImage *)image //{ // self.imageView.image = image; //} - (void)download2 { // 图片的网络路径 NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"]; CFTimeInterval begin = CFAbsoluteTimeGetCurrent(); // 根据图片的网络路径去下载图片数据 NSData *data = [NSData dataWithContentsOfURL:url]; CFTimeInterval end = CFAbsoluteTimeGetCurrent(); NSLog(@"%f", end - begin); // 显示图片 self.imageView.image = [UIImage imageWithData:data]; } - (void)download { // 图片的网络路径 NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"]; NSDate *begin = [NSDate date]; // 根据图片的网络路径去下载图片数据 NSData *data = [NSData dataWithContentsOfURL:url];
    NSDate
    *end = [NSDate date]; NSLog(@"%f", [end timeIntervalSinceDate:begin]); // 显示图片 self.imageView.image = [UIImage imageWithData:data]; } @end

    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    VScode 最新eslint设置
    vue部署在二级目录
    实现网页比12像素小
    css一个很好用的hover显示
    VSCode设置
    vue发布后的一些问题
    设置cssrem,设置emmet
    js时间与时间戳之间的转换操作,返回天、小时、分,全家桶
    Error resolving template [xxx], template might not exist or might not be exist
    解决jenkins shell执行sonar-scanner提示命令存在的问题
  • 原文地址:https://www.cnblogs.com/laugh/p/6553607.html
Copyright © 2011-2022 走看看