首页 > 移动平台 > 详细

IOS第二天多线程-02NSThread基本使用

时间:2015-09-08 18:19:28      阅读:248      评论:0      收藏:0      [点我收藏+]

****

#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

 

IOS第二天多线程-02NSThread基本使用

原文:http://www.cnblogs.com/ios-g/p/4792483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!