NSThread

来源:互联网 发布:java迭代器用法 编辑:程序博客网 时间:2024/05/21 21:34

同步Demo

//NSURlSe

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@”%@”,[NSThread currentThread]);

    //创建线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread1:) object:@”123”];

    //alloc创建的线程 必须手动启动线程
    [thread start];

    //+方法 自动启动
    [NSThread detachNewThreadSelector:@selector(thread3) toTarget:self withObject:nil];

    //
    [self performSelectorInBackground:@selector(thread2) withObject:nil];
    }

-(void)thread3
{
NSLog(@”333%@”,[NSThread currentThread]);

[NSThread exit];

}

-(void)thread2
{

NSLog(@"222%@",[NSThread currentThread]);

}

-(void)thread1:(id)obj
{
NSLog(@”111%@ —> %@”,[NSThread currentThread],obj);
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

//多任务 —> 进程 资源独立 –> 线程 资源共享
//ios APP 系统 自动创建一个线程 主线程(刷新UI 和 处理事件)(1MB) —> 刷新UI 全部放到主线程
//提高程序的执行的效率 //3-5 子线程 (512KB)
//代码的逻辑会更复杂 —> a //死锁

//ARC MRC
//ios
//C PThread
//OC NSThread 创建 销毁
//队列 和 任务
//C GCD
//OC NSOperation —> GCD

-(void)mySleep
{
sleep(10);
}

  • (IBAction)click:(id)sender {

    //
    [NSThread detachNewThreadSelector:@selector(mySleep) toTarget:self withObject:nil];
    }

线程同步

//nonatomic atomic

//nonatomic —> 单线程
/*
{
_ticketCount = ticketCount;
}
*/

/*
atomic
{
@synchronized(self) {
_ticketCount = ticketCount;
}
}
*/

@property (atomic) NSInteger ticketCount;

@property (nonatomic,strong) NSLock *lock;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.ticketCount = 100;

    self.lock = [[NSLock alloc] init];

    for (NSInteger i = 0; i < 3; i++) {
    [NSThread detachNewThreadSelector:@selector(saleTicket) toTarget:self withObject:nil];
    }
    }

if 0
-(void)saleTicket
{
while (1) {

    //锁    //加锁    [self.lock lock];    if(self.ticketCount > 0)    {        self.ticketCount --;        NSLog(@"%ld",self.ticketCount);    }    else    {        break;    }    [self.lock unlock];}

}
endif

-(void)saleTicket
{
while (1) {
//锁
//加锁
//如果一个线程使用该资源加锁,那么另外一个线程要使用该资源 必须等待前一个线程 释放该资源
@synchronized(self) {
if(self.ticketCount > 0)
{
self.ticketCount –;
NSLog(@”%ld”,self.ticketCount);
}
else
{
break;
}
}
}
}

开启线程,下载图片

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

// NSLog(@”%d”,[NSThread isMainThread]);
//
// NSURLSession *s = [NSURLSession sharedSession];
//
// [[s dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”http://d.hiphotos.baidu.com/news/q%3D100/sign=c72ee1e8d51b0ef46ae89c5eedc651a1/b8014a90f603738dca9ba5e4b41bb051f919ec27.jpg“]] completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
// //self.imageView.image = [UIImage imageWithData:data];
// [self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:NO];
// }] resume];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(void)updateUI:(NSData *)data
{
NSLog(@”%d”,[NSThread isMainThread]);
//主线程
self.imageView.image = [UIImage imageWithData:data];
}

-(void)downImage
{
//子线程
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”http://d.hiphotos.baidu.com/news/q%3D100/sign=c72ee1e8d51b0ef46ae89c5eedc651a1/b8014a90f603738dca9ba5e4b41bb051f919ec27.jpg“]];

//通知主线程 刷新UI  子线程刷新UI可能会有延时.[self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:NO];

}

  • (IBAction)download:(id)sender {

    [NSThread detachNewThreadSelector:@selector(downImage) toTarget:self withObject:nil];

}

0 0
原创粉丝点击