多线程01-NSThread 基本了解和一些总结1

来源:互联网 发布:无线信道检测软件 编辑:程序博客网 时间:2024/05/29 09:34
零、线程的注意点(掌握)1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示、刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程,异步线程3.不要把耗时的操作放在主线程,要放在子线程中执行一、NSThread(掌握)1.创建和启动线程的3种方式1> 先创建,后启动// 创建NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil];// 启动[thread start];2> 创建完自动启动[NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];3> 隐式创建(自动启动)[self performSelectorInBackground:@selector(download:) withObject:nil];2.常见方法1> 获得当前线程+ (NSThread *)currentThread;2> 获得主线程+ (NSThread *)mainThread;3> 睡眠(暂停)线程+ (void)sleepUntilDate:(NSDate *)date;+ (void)sleepForTimeInterval:(NSTimeInterval)ti;4> 设置线程的名字- (void)setName:(NSString *)n;- (NSString *)name;二、线程同步(掌握)1.实质:为了防止多个线程抢夺同一个资源造成的数据安全问题2.实现:给代码加一个互斥锁(同步锁)@synchronized(self) {    // 被锁住的代码}

这里写图片描述

这里写图片描述

////  HMViewController.m//  03-NSThread01-基本使用(了解)////  Created by apple on 14-9-15.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMViewController.h"@interface HMViewController ()@end@implementation HMViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)download:(NSString *)url{    NSLog(@"下载东西---%@---%@", url, [NSThread currentThread]);}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self createThread3];}/** * 创建线程的方式3 */- (void)createThread3{    // 这2个不会创建线程,在当前线程中执行//    [self performSelector:@selector(download:) withObject:@"http://c.gif"];//    [self download:@"http://c.gif"];    [self performSelectorInBackground:@selector(download:) withObject:@"http://c.gif"];}/** * 创建线程的方式2 */- (void)createThread2{    [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://a.jpg"];}/** * 创建线程的方式1 */- (void)createThread1{    // 创建线程    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:@"http://b.png"];    thread.name = @"下载线程";    // 启动线程(调用self的download方法)    [thread start];}@end
////  HMViewController.m//  04-NSThread02-线程状态(了解)////  Created by apple on 14-9-15.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMViewController.h"@interface HMViewController ()@property (nonatomic, strong) NSThread *thread;@end@implementation HMViewController- (void)viewDidLoad{    [super viewDidLoad];    self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];    // Do any additional setup after loading the view, typically from a nib.}- (void)download{    NSLog(@"-----begin");    // 睡眠5秒钟//    [NSThread sleepForTimeInterval:5];    // 3秒后的时间//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3];//    [NSThread sleepUntilDate:date];    for (int i = 0; i<100; i++) {        NSLog(@"------%d", i);//        return;//        if (i == 49) {//            [NSThread exit];//        }    }    NSLog(@"-----end");}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//    [self.thread start];    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];    [thread start];}@end
0 0