• iOS多线程开发小demo4,线程的同步问题


    //  DYFViewController.m
    //  623-05-线程同步问题
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
    
    #import "DYFViewController.h"
    
    @interface DYFViewController ()
    @property (nonatomic, assign) int leftCount;
    
    @property (nonatomic, strong) NSThread *thread0;
    @property (nonatomic, strong) NSThread *thread1;
    @property (nonatomic, strong) NSThread *thread2;
    
    @end
    
    @implementation DYFViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        
        // 默认有100张
        self.leftCount = 100;
        // 开启多条线程同时卖票
        self.thread0 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:@"000"];
        self.thread0.name = @"0000";
        // 优先级
        self.thread0.threadPriority = 1.0;
        
        self.thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:@"000"];
        self.thread1.name = @"1111";
        self.thread1.threadPriority = 0.0;
        
        self.thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:@"000"];
        self.thread2.name = @"2222";
        self.thread2.threadPriority = 0.0;
        
        
    }
    
    /**
     *  卖票
     */
    - (void)saleTicket
    {
    //    NSLock *lock = [[NSLock alloc] init];
    //    lock.tryLock
    //    lock.lock
        
        while (1) {
        
            @synchronized(self){ // 开始枷锁
            
            // 1.先检查票数
            int count = [self leftCount];
            if (count > 0) {
                // 票数 - 1
                [self setLeftCount:(count - 1)];
                // 暂停
                [NSThread sleepForTimeInterval:0.0004];
                NSThread *currentT = [NSThread currentThread];
                NSLog(@"%@,-------%d", currentT.name, self.leftCount);
            }else
            {
                // 退出线程
                [NSThread exit];
            }
            } // 解锁
        }
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.thread0 start];
        [self.thread1 start];
        [self.thread2 start];
    }
    
    @end
    

     小结:

    --------------线程的安全问题(多线程的安全隐患)-------

    1.资源共享

    ·一块资源可能会被多个线程共享,即多个线程可能会访问同一块资源

    ·比如多个线程访问同一个对象,同一个变量、同一个文件

    2.当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题

    3.实例

    eg。1 存钱取钱

    eg。2 卖票

    -----------------安全隐患解决-互斥锁-----

    1.格式

    @synchronized(锁对象)

    {// 需要锁定的代码

    }

    注意:锁定一份代码只能用1把锁,用多把锁是无效的

    2.优缺点

    ·优点:能有效防止多线程抢夺资源造成的安全问题

    ·缺点:需要消耗大量的cpu资源

    3.互斥锁的使用前提:多条线程抢夺同一块资源

    4.相关术语:线程同步

    ·means:多条线程按顺序的执行任务

    ·互斥锁就是使用了线程同步技术

  • 相关阅读:
    记录操作:增删改查
    mysql表的完整性约束
    mysql支持的数据类型:数值类型、时间类型、字符串类型、ENUM和SET类型
    mysql中的存储引擎
    系统从未分库分表动态切换到分库分表
    为什么要分库分表
    生产环境中的 redis 是怎么部署的
    redis 集群模式
    redis 的持久化方式
    redis 的过期策略都有哪些?内存淘汰机制都有哪些?
  • 原文地址:https://www.cnblogs.com/dyf520/p/3805306.html
Copyright © 2020-2023  润新知