IOS中的多线程

类目 延展 协议  继承
1.一个应用程序一个进程 一个进程包含多个线程
2.线程和进程可以共享同一块内存区域
3.异步请求 xml 解析 在子线程中跑  ,打开程序就会给我们创建一个线程  其他的称为子线程
4.IOS中关于UI任何操作必须在主线程中操作
多线程应用
5.for 循环进行大量运算
6.数据库查询
7.网络 请求(同步)
8.解析
NSThread   
NSOperation  NSOperationQueue 线程队列
NSObject
GCD
MainViewController.h

@interface MainViewController : UIViewController{

    UIImageView *_aImage;

}


MainViewController.m

-(void)createOperation{

    

    //同时运行的线程叫并发线程  最大线程数不超过10

    //非协议网络链接都是同步加载 卡主线程

    CustomOpe *nsthread = [[CustomOpe alloc]init] ;

    //[nsthread start];

    

    NSOperationQueue *nsoperation = [[NSOperationQueue alloc]init];

    [nsoperation addOperation:nsthread];

   CustomOpe *nsthread1 = [[CustomOpe alloc]init] ;

    [nsoperation addOperation:nsthread1];

    [nsoperation setMaxConcurrentOperationCount:1];//最大并发数为1是线程的同步

    [nsthread release];

    [nsthread1 release];

    

}

//创建子线程

#pragma mark -

#pragma mark NSThread

-(void)createThread{

    //当前的方法是否在主线程中运行[进程号:线程号]

    if ([NSThread   isMainThread]) {

        NSLog(@"主线程");

    }

//    NSThread *nsthread = [[NSThread alloc]initWithTarget:self selector:@selector(createAddNumber:) object:nil];

//    [nsthread start];

    [NSThread detachNewThreadSelector:@selector(createAddNumber:) toTarget:self withObject:nil];

    

}


- (void)createAddNumber:(id)sender{

    //自动释放池

   // 早期的写法

   // NSAutoreleasePool *pool = [NSAutoreleasePool alloc];

    @autoreleasepool {

//        

//        for (int i =0; i<6355000; i++) {

//            

//            NSLog(@"i =%d",i);

//        }

       // [pool drain];

        NSURL *url =[NSURL URLWithString:@"http://img.chengmi.com/cm/021da391-512b-41e7-af65-cfaaecb19654.jpg"];

        NSData *data = [NSData dataWithContentsOfURL:url];

        UIImage *currentImage = [UIImage imageWithData:data];

        //把操作ui必须返回主线程  将图片传过去

        [self performSelectorOnMainThread:@selector(changeImageview:) withObject:currentImage waitUntilDone:YES];

    }

}

-(void)changeImageview:(UIImage *)image{

    //把图片

    [_aImage setImage:image];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end



@implementation CustomOpe

- (void)dealloc

{

    [super dealloc];

}

-(id)init{

    self = [super init];

    if (self) {

        

    }

    return  self;

}


//重写Mian方法  程序调用完init 方法后,立刻执行的该方法,做多线程操作


- (void)main{

    @autoreleasepool {

        [self ope];

       

    }

}

- (void)ope{

    for ( int i =0; i<10; i++) {

        NSLog(@"%d",i);

    }

}

@end



- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent = NO;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 70, 100, 100);

    [button setTitle:@"点击" forStateUIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted ];

    button.backgroundColor = [UIColor redColor];

    [button addTarget:self action:@selector(createConcurrentGCD) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

    _aImage= [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 200, 100)];

    [self.view addSubview:_aImage];

    [_aImage release];

}

//创建一个并发的GCD线程队列

-(void)createConcurrentGCD{

    //异步执行并发线程队列 参数一 执行级别   参数二  系统的保留参数

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSURL * url = [NSURL URLWithString:@"http://img.chengmi.com/cm/021da391-512b-41e7-af65-cfaaecb19654.jpg"];

        NSData *data = [NSData dataWithContentsOfURL:url];

        UIImage *currentImage = [UIImage imageWithData:data];

//异步返回主线程

        dispatch_async(dispatch_get_main_queue(), ^{

            [_aImage setImage:currentImage];

        });

    });

    

}

- (void)createGCD{

    //创建一个线程队列 参数1 队列名字是first  参数二 选择队列类型 DISPATCH_QUEUE_SERIAL  现在是同步的线程队列

    dispatch_queue_t firstQueue = dispatch_queue_create("first", DISPATCH_QUEUE_SERIAL);

    //异步执行线程队列

    dispatch_async(firstQueue, ^{

        //数据库查询  网络请求都可以写

        for (int i=0; i<10; i++) {

            NSLog(@"i===%d",i);

        }

    });//这两个叫同步线程队列

    //  dispatch_async 代表对于主线程来说是异步执行后面的方法

    //firstQueue 是针对与它本身来说是一个同步队列

    //block 是它本身添加到firstQueue 这同步队列里面执行

    dispatch_async(firstQueue, ^{

        //数据库查询  网络请求都可以写

        for (int i=0; i<10; i++) {

            NSLog(@"%d",i);

        }

    });


    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值