//
// ZYOperationViewController.h
// Thread
//
// Created by yejiong on 14
//
// ZYOperation.h
// Thread
//
// Created by yejiong on 14/11/4.
// Copyright © 2014年 zzz. All rights reserved.
// #import <Foundation/Foundation.h> @interface ZYOperation : NSOperation @property (nonatomic, copy) NSString* operationName; @end

//
// ZYOperation.m
// Thread
//
// Created by yejiong on 14/11/4.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ZYOperation.h" @implementation ZYOperation - (void)dealloc {
[_operationName release]; [super dealloc];
} //重写 main 方法,在这里写入我们需要在线程中执行的代码。
- (void)main {
// NSLog(@"--------%@", [NSThread currentThread]);
//
// if ([NSThread isMainThread]) {
// NSLog(@"主线程");
// }else {
// NSLog(@"分线程");
// } NSLog(@"%@", _operationName);
} @end
/11/4.
// Copyright © 2014年 zzz. All rights reserved.
// #import <UIKit/UIKit.h> @interface ZYOperationViewController : UIViewController @end
//
// ZYOperationViewController.m
// Thread
//
// Created by yejiong on 14/11/4.
// Copyright © 2014年 zzz. All rights reserved.
// #import "ZYOperationViewController.h"
#import "ZYOperation.h" @interface ZYOperationViewController () {
NSOperationQueue* _operationQueue; } @property (nonatomic, retain) NSBlockOperation* blockOperation1; @end @implementation ZYOperationViewController - (void)dealloc {
[_operationQueue release]; [_blockOperation1 release]; [super dealloc];
} - (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor]; _operationQueue = [[NSOperationQueue alloc] init]; //设置最大并发数,同时最多有多少线程在执行。
_operationQueue.maxConcurrentOperationCount = ; //使用 NSBlockOperation 的时候需要注意是否造成循环引用。
__block ZYOperationViewController* blockSelf = self; self.blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
blockSelf.view.backgroundColor = [UIColor redColor];
}]; //没有造成循环引用。
NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
self.view.backgroundColor = [UIColor redColor];
}];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//NSOperation 是一个抽象类,我们不能使用它的对象,必须使用它子类的对象。
//系统提供了两个子类:NSInvocationOperation,NSBlockOperation。
//我们也可以自定义 NSOperation 子类,重写 mian 方法,然后把 operation 对象添加到 NSOperationQueue 中,就会自动在分线程执行 main 方法。 // [self invocationOperation]; // [self blockOperation];

//自定义
[self customOperation];
} - (void)invocationOperation {
NSInvocationOperation* invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@""]; //使用 start 在主线程执行任务。
// [invocationOperation start]; //如果需要在分线程执行任务,把对象添加到 NSOperationQueue,就会自动的在分线程执行任务。 [_operationQueue addOperation:invocationOperation]; [invocationOperation release];
} - (void)blockOperation {
NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1--------%@", [NSThread currentThread]); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
}
}]; //主线程
// [blockOperation start]; //分线程
// [_operationQueue addOperation:blockOperation]; [blockOperation addExecutionBlock:^{
NSLog(@"2--------%@", [NSThread currentThread]); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
}
}]; [blockOperation addExecutionBlock:^{
NSLog(@"3--------%@", [NSThread currentThread]); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
}
}]; [blockOperation addExecutionBlock:^{
NSLog(@"4--------%@", [NSThread currentThread]); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
}
}]; [blockOperation addExecutionBlock:^{
NSLog(@"5--------%@", [NSThread currentThread]); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
}
}]; //addExecutionBlock,并发的执行多个 block 任务,系统自动分配到当前空闲的线程,可能是主线程也可能是分线程。
// [blockOperation start]; //全部在分线程并发执行。
[_operationQueue addOperation:blockOperation];
} - (void)customOperation {
ZYOperation* operation = [[ZYOperation alloc] init]; operation.operationName = @""; ZYOperation* operation1 = [[ZYOperation alloc] init]; operation1.operationName = @""; ZYOperation* operation2 = [[ZYOperation alloc] init]; operation2.operationName = @""; //设置线程间的依赖关系。让一个线程等待另外一个线程执行完毕再开始执行。
[operation addDependency:operation1]; [_operationQueue addOperation:operation];
[_operationQueue addOperation:operation1];
[_operationQueue addOperation:operation2]; [operation release];
[operation1 release];
[operation2 release]; } - (void)run:(id)object {
NSLog(@"%@", object); if ([NSThread isMainThread]) {
NSLog(@"主线程");
}else {
NSLog(@"分线程");
} //仍然使用这个方法回到主线程。
// self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>
} @end

线程2 NSOperation 抽像类的使用的更多相关文章

  1. 接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)?

    接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)? 1. 接口可以继承接口. 2. 抽像类可以实现(implements)接 ...

  2. php面向对象之抽像类、接口、final、类常量

    一.抽像类(abstract)        在我们实际开发过程中,有些类并不需要被实例化,如前面学习到的一些父类,主要是让子类来继承,这样可以提高代码复用性语法结构:  代码如下 复制代码   ab ...

  3. Java并发包——线程安全的Collection相关类

    Java并发包——线程安全的Collection相关类 摘要:本文主要学习了Java并发包下线程安全的Collection相关的类. 部分内容来自以下博客: https://www.cnblogs.c ...

  4. Java并发包——线程安全的Map相关类

    Java并发包——线程安全的Map相关类 摘要:本文主要学习了Java并发包下线程安全的Map相关的类. 部分内容来自以下博客: https://blog.csdn.net/bill_xiang_/a ...

  5. NSThread 子线程 Cocoa NSOperation GCD(Grand Central Dispatch) 多线程

    单词:thread 英 θred:n 线.思路.vt 穿过.vi 穿透过 一.    进程.线程 进程:正在进行中的程序被称为进程,负责程序运行的内存分配,每一个进程都有自己独立的虚拟内存空间 线程: ...

  6. 创建线程方式-NSOperation

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. c#跨线程访问控件帮助类

    1.背景 对于winform程序来说,当我们点击按钮,需要消耗一定时长才能拿到数据后才能显示在界面上某个控件上的情况,我们通常会专门开一个线程去拿数据,这样不会造成界面处于假死状态 2.常规做法 // ...

  8. java 之前的安全的类回顾,以及以后需要线程安全时使用哪些类

    之前所学习到的线程安全的类: StringBuffer:线程安全的可变字符序列.一个类似于 String 的字符串缓冲区,但不能修改. Vector:Vector 类可以实现可增长的对象数组. Has ...

  9. JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)

    实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...

随机推荐

  1. Java IO流中的File类学习总结

    一.File类概述 File类位于java.io包中,是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹. File类有多种重载的构造方法.File类保存文件或目录的各种 ...

  2. 对unsigned int和int进行移位操作的区别

    1. 无符号整数 unsigned int 对unsigned int进行移位操作时,最高位不会有任何特殊性. 无符号整数必须使用%u来打印 #include <stdio.h> int ...

  3. Windows Server 2012 四个版本对比

    Windows Server 2012 有4种版本: Foundation, Essentials, Standard and Datacenter. 版本 Foundation Essentials ...

  4. Myeclipse 快捷键设置和自动提示设置

    1.快捷键设置和调整 2.自动提示信息设置与调整

  5. ElasticSearch+Springboot实际应用:索引同步建设,搜索过程

    1.介绍 springboot框架,众多自动化的部署和约定配置,造成了springboot的着手麻烦,熟练后可以快速快捷进行开发,常用作快捷开发的java底层框架.各位看官都是大神,自行体会.     ...

  6. socket的shutdown与close

    shutdown原型 int shutdown(int socket, int how); socket 指定socket的文件描述符 how    指定shutdown的类型 SHUT_RD 禁止读 ...

  7. Spring和Hibernate集成的HibernateTemplate的一些常用方法总结

    1:get/load存取单条数据 public Teacher getTeacherById(Long id) { return (Teacher)this.hibernateTemplate.get ...

  8. 对CAB文件进行数字签名

    对CAB文件进行数字签名 传说中数字签名之后就能够不出现提示而自己主动下载,所以也试试: 在\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin 中间有三个小 ...

  9. [ES6] 19. for ... of

    In ES5, we have for ... in: var phones = ["iPhone", "Nexus", "Nokia"]; ...

  10. JavaScript提高:003:easy UI实现tab页面自适应问题

    前面说到使用easyUI在asp.net中实现了tab控件效果.http://blog.csdn.net/yysyangyangyangshan/article/details/38307477只是有 ...