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

零、线程的注意点(掌握)
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值