iOS开发多线程篇—创建线程
创建和启动线程简单说明
(1)线程的执行顺序由CPU负责,程序员不能保证线程的执行顺序,不能相信一次执行的结果
NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"hello I am threadA"];
(2)创建线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式创建并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述(2)(3)创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置
代码演示
//
// ViewController.m
// 02NSThread
//
// Created by mac on 15/10/9.
// Copyright © 2015年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//移除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWillBecomeMultiThreadedNotification object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// FOUNDATION_EXPORT NSString * const NSWillBecomeMultiThreadedNotification;
// FOUNDATION_EXPORT NSString * const NSDidBecomeSingleThreadedNotification;
// FOUNDATION_EXPORT NSString * const NSThreadWillExitNotification;
//注册通知(NSWillBecomeMultiThreadedNotification,是通知的名字,又系统发送,在程序开启子线程的时候发送该通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeMutipleThread) name:NSWillBecomeMultiThreadedNotification object:nil];
}
- (void)becomeMutipleThread
{
NSLog(@"开启多线程");
}
//触摸屏幕执行此方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/*
// [self testOne];
if ([NSThread isMultiThreaded]) {
NSLog(@"是多线程");
}else{
NSLog(@"单线程");
}//单线程
// [self testTwo];
[self threadProperty];
if ([NSThread isMultiThreaded]) {
NSLog(@"是多线程");
}else{
NSLog(@"单线程");
}//多线程
*/
[self nsobjectThread];
// [self test];//在主线程中调用test方法
}
//线程的优先级
- (void)threadProperty
{
//线程的执行顺序由CPU负责,程序员不能保证线程的执行顺序,不能相信一次执行的结果
NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"hello I am threadA"];
threadA.name = @"ThreadA";
//优先级:0.0~1.0,1.0是最高优先级
[threadA setThreadPriority:1.0];
[threadA start];
NSThread *threadB = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"hello I am threadB"];
threadB.name = @"ThreadB";
[threadB start];
}
- (void)nsobjectThread{
//开启一个子线程调用longOperation:方法
[self performSelectorInBackground:@selector(longOperation:) withObject:@"NSObject method"];
//在主线程中调用
// [self longOperation:@"main thread"];
}
//开启子线程2
- (void)testTwo {
//开启一个子线程,并且执行
[NSThread detachNewThreadSelector:@selector(longOperation:) toTarget:self withObject:@"hello"];
}
//开启子线程1
- (void)testOne {
// NSThread
//产生线程对象threadA----[target selector:argument]
//- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument;
NSThread *threadA = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"hello I am threadA"];
threadA.name = @"ThreadA";
[threadA start];
}
//耗时操作
- (void)longOperation:(id)obj
{
for (int i = 0 ; i < 10 ; i ++) {
// if (i % 2 == 0) {
//线程休眠
// + (void)sleepUntilDate:(NSDate *)date;休眠到某一个时间点
// + (void)sleepForTimeInterval:(NSTimeInterval)ti;休眠多久
// NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:2];
// [NSThread sleepForTimeInterval:2];
// [NSThread sleepUntilDate:date1];
// }
NSLog(@"当前线程---%@---%d--%@",[NSThread currentThread],i,obj);
// if (i == 5) {
// [NSThread exit];
// }
}
// [self test];//在当前线程中调用
[self performSelectorOnMainThread:@selector(test) withObject:self waitUntilDone:YES];
// self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#> modes:<#(nullable NSArray<NSString *> *)#>
}
- (void)test {
NSLog(@"%s 当前线程---%@",__FUNCTION__,[NSThread currentThread]);
}
//1.注意,任何的UI操作必须在主线程中进行。
//2.应用程序的资源分配是以进程为单位
@end
浙公网安备 33010602011771号