222,NSThread类的使用

来源:互联网 发布:mac相册制作软件 编辑:程序博客网 时间:2024/06/10 05:20

1,创建线程

(1)使用NSThread类中initWithTarget:创建线程

创建后,需要手动启动线程

NSThread *thread1 = [[NSThreadalloc]initWithTarget:selfselector:@selector(run:)object:@"http:www.baidu.com"];

   //启动线程

   [thread1 start]

(2)使用NSThread类中detachNewThreadSelector:创建线程

        创建线程后,自动启动线程

   [NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"http://www.hao123.com"];


(3)隐形创建线程

      创建线程后,自动启动线程

   [selfperformSelectorInBackground:@selector(run:)withObject:@"http://www.google.com"];

    - (void)run:(NSString *)url{

       NSLog(@"-----%@-----%@-----%@",[NSThreadcurrentThread],[NSThreadmainThread],url);

    }


2,NSThread的常用属性与方法

(1)获取当前线程

  + (NSThread *)currentThread;

(2)获取主线程

  + (NSThread *)mainThread;

(3)退出线程

  + (void)exit;

(4)线程睡眠

  + (void)sleepUntilDate:(NSDate *)date;

  + (void)sleepForTimeInterval:(NSTimeInterval)ti;

  eg:

  //睡眠五秒,线程阻塞五秒

  [NSThreadsleepForTimeInterval:5];

   或

  NSDate *date = [NSDatedateWithTimeIntervalSinceNow:5];

    [NSThreadsleepUntilDate:date];

(5)设置线程的优先级

  + (double)threadPriority;

  + (BOOL)setThreadPriority:(double)p;

范围为0-1,默认情况下,优先级为0.5,数值越大,线程越先被调用

  @property double threadPriority


(6)设置线程的名称

  @property (nullable,copy)NSString *name

(7)是不是主线程

  + (BOOL)isMainThread

3,线程同步

(1)实质:为了防止多个线程抢夺同一个资源造成的数据安全问题

(2)解决方案:给代码加一个互斥锁(同步锁)

  • 互斥锁使用格式

  @synchronized(锁对象) {//需要锁定的代码  }

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


eg: 

#define myStrong(name) @property (nonatomic,strong) NSThread *name;


#import "ViewController.h"


@interface ViewController ()


@property (nonatomic,assign)int ticketCount;

myStrong(thread1);

myStrong(thread2);

myStrong(thread3);


@end


@implementation ViewController


- (void)viewDidLoad{

    self.ticketCount =100;

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.thread1 = [[NSThreadalloc] initWithTarget:selfselector:@selector(buyTicket)object:nil];

    self.thread1.name =@"thread1";

    self.thread2 = [[NSThreadalloc] initWithTarget:selfselector:@selector(buyTicket)object:nil];

    self.thread2.name =@"thread2";

    self.thread3 = [[NSThreadalloc] initWithTarget:selfselector:@selector(buyTicket)object:nil];

    self.thread3.name =@"thread3";

    [self.thread1start];

    [self.thread2start];

    [self.thread3start];

}



- (void)buyTicket{

    [NSThreadsleepForTimeInterval:0.2];

    

    while (1) {

        @synchronized(self){

        int count = self.ticketCount;

        if (count > 0) {

            self.ticketCount = count -1;

            NSLog(@"当前线程的名字为%@,卖出一张,剩余的票 %i",[NSThreadcurrentThread].name,self.ticketCount);

        }else{

            return;

        }

        }}

}


@end


4,原子属性与非原子属性

  • nonatomicatomic对比
  • atomic:线程安全,需要消耗大量的资源
  • nonatomic:非线程安全,适合内存小的移动设备
  • iOS开发的建议
  • 所有属性都声明为nonatomic
  • 尽量避免多线程抢夺同一块资源
  • 尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力


注:线程执行完就死亡了,不能再次start;若要重新执行同样的操作,则需要重新创建线程对象。以下为线程生命周期,

0 0
原创粉丝点击