zoukankan      html  css  js  c++  java
  • 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:多条线程按顺序的执行任务

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

  • 相关阅读:
    不常用的cmd命令
    js获取宽度
    Marshaling Data with Platform Invoke 概览
    Calling a DLL Function 之三 How to: Implement Callback Functions
    Marshaling Data with Platform Invoke 之四 Marshaling Arrays of Types
    Marshaling Data with Platform Invoke 之一 Platform Invoke Data Types
    Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)
    Calling a DLL Function 之二 Callback Functions
    WCF 引论
    Marshaling Data with Platform Invoke 之二 Marshaling Strings (用时查阅)
  • 原文地址:https://www.cnblogs.com/dyf520/p/3805306.html
Copyright © 2011-2022 走看看