终于只剩下最后不到一个月的时间,日子在我们的学习中悄然而逝,最后的课程却越来越多,也更加疲累,因为课程进度有所延迟,从而导致最后的日子越加急促,这个星期开始接触iOS高级,知道了一些线程和部分网络相关的知识,不知是老师讲课速度太快,还是我接收能力太弱,每次都感觉落下好多,只能等到自习或放假的时间才能补全回来,最近上课经常走神,不是突然想到之前没写好的代码就是想到家人,我不知为何,却有种暖暖感动在心中缓缓流淌,眼眶时有隐隐泪花,不管如何我都一定要把iOS学好,为了家人!
// ———————————NSThread 线程———————————
NSThread
*thread = [[
NSThread
alloc
]
initWithTarget
:
self
selector
:
@selector
(method1)
object
:
nil
]; // 1、NSThread创建一个线程
[thread start]; // 启动线程
[
self
performSelectorInBackground
:
@selector
(method1)
withObject
:
nil
];
// 2、在后台执行任务,线程就绪,在适合的时间点进入运行状态
[
NSThread
detachNewThreadSelector
:
@selector
(method1)
toTarget
:
self
withObject
:
nil
]; // 3、使用NSThread类方法开启新线程
NSThread
*mainThread = [
NSThread
currentThread
];
// 获取到当前方法的执行线程
thread.
threadPriority // 优先级
[mainThread
isMainThread
]
// 判断是否主线程
[
NSThread
isMultiThreaded
]
// 判断是否多线程
[
NSThread
sleepForTimeInterval
:
10
];
// 线程阻塞
[myThread
cancel
];
// 标记为取消状态
[
NSThread
exit
];
// 停止、退出
[[
NSThread
currentThread
]
isCancelled
]
// 找到cancelled方法后执行退出
[
self
performSelectorOnMainThread
:
@selector
(setImage:)
withObject
:image
waitUntilDone
:
YES
]; // 回到主线程上个执行操作
CustomThread *ct2 = [[CustomThread alloc] initWithTarget:self selector:@selector(method1) object:nil]; // 继承NSThread类
[ct2
start
];
// 重写后调用[super start]会自动调用main方法
[ct2
main
];
// 线程执行调用这个方法
}
// ———————————NSLock 锁———————————
_lock
= [[
NSLock
alloc
]
init
];
// 创建锁对象
[
_lock
lock
];
// 加锁
[
_lock
unlock
];
// 解锁
@synchronized
(
self
) {
// 给线程加锁,保证资源同时只能被一个线程访问
}
// nonatomic 非原子性
// atomic 原子性 会在set方法中给属性加锁,消耗一定的性能
@property
(
atomic
,
assign
)
int
count;
// ———————————NSOperation 操作———————————
// 1、主队列:操作在主线程上执行
NSOperationQueue *mainQueue = [ NSOperationQueue mainQueue ];
NSOperationQueue *mainQueue = [ NSOperationQueue mainQueue ];
[mainQueue addOperationWithBlock:^{ // 添加操作
}];
// 2、其他队列:操作在其他线程上执行(多线程)
NSOperationQueue *queue= [[ NSOperationQueue alloc ] init ];
// 2、其他队列:操作在其他线程上执行(多线程)
NSOperationQueue *queue= [[ NSOperationQueue alloc ] init ];
[queue addOperationWithBlock:^{
}];
//
-----------------------
系统提供的
NSOperation子类操作对象
-----------------------
// 1、NSBlockOperating
NSBlockOperation *op = [ NSBlockOperation blockOperationWithBlock :^{
NSLog ( @"blockOperation 1" );
}];
[op
addExecutionBlock
:^{
// 在op上继续添加一个操作
NSLog
(
@"op "
);
}];
[queue addOperation :op]; // 添加到操作列队
// 2、NSinvocationOperation
NSInvocationOperation *op2 = [[ NSInvocationOperation alloc ] initWithTarget : self selector : @selector (invocationOperation) object : nil ];
[queue addOperation :op2];
}
- ( void )invocationOperation {
NSLog ( @"op2" );
}
// -----------------------自定义NSOperation子类操作对象-----------------------
NSOperationQueue *queue2 = [[NSOperationQueue alloc] init];
MyOperation *myQueue = [[MyOperation alloc] init]; // 继承NSOperation 类
/*
1、myQueue操作需要添加到main方法中,当main返回的时候操作执行结束
2、覆写start方法,需要调用[super start]方法,会自动调用main方法
1、myQueue操作需要添加到main方法中,当main返回的时候操作执行结束
2、覆写start方法,需要调用[super start]方法,会自动调用main方法
*/
[queue2 addOperation:myQueue]; // 添加操作
// -----------------------NSOperaion 操作顺序-----------------------
NSOperationQueue *queue = [[ NSOperationQueue alloc ] init ]; // 队列
NSOperationQueue *queue = [[ NSOperationQueue alloc ] init ]; // 队列
[queue setSuspended:YES];
// 队列暂停执行操作
queue.
maxConcurrentOperationCount
=
1
;
// 最大并行操作数量,如果设置为1,同时只有一个操作执行,具体在哪个线程不确定
NSBlockOperation *op1 = [ NSBlockOperation blockOperationWithBlock :^{ // 操作1
for ( int i = 0 ; i <= 10 ; i ++) {
NSLog ( @"op1 : %d" ,i);
};
}];
NSBlockOperation *op2 = [ NSBlockOperation blockOperationWithBlock :^{ // 操作2
for ( int i = 0 ; i <= 10 ; i ++) {
NSLog ( @"op2 : %d" ,i);
};
}];
NSBlockOperation *op3 = [ NSBlockOperation blockOperationWithBlock :^{ // 操作3
for ( int i = 0 ; i <= 10 ; i ++) {
NSLog ( @"op3 : %d" ,i);
};
}];
[queue addOperation:op1]; //如果最大并行数量为1,则先添加的先执行,如果队列没有暂停或其他操作,添加一个操作就会马上执行。
[op3
setQueuePriority
:
NSOperationQueuePriorityHigh
];
// 设置操作的优先级
[op3
addDependency
:op1];
// 设置操作依赖于其他操作(op3依赖于op2)
[queue setSuspended:NO]; // 取消暂停
无限互联官网: http://www.iphonetrain.com/
无限互联官网: http://www.iphonetrain.com/