ios线程 线程保护 卖票问题 网络加载的线程

1 篇文章 0 订阅

1.创建线程的三种方式 (队列线程)

1.1block创建线程

NSBlockOperation *operation = [NSBlockOperationblockOperationWithBlock:^{

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

                    NSLog(@"thread =   = %@",[NSThreadcurrentThread]);

                }

            }];

            //开启线程

            [operation start];


 1.2用类方法创建线程(包含两种方式+对象创建) 类方法创建不用 打开  因为系统为我们自动打开

1.21 对象方法

NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"];

                [thread start];

1.22 类

 [NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"线程B"];

1.23 类

[selfperformSelectorInBackground:@selector(downLoadImage)withObject:nil];

1.3用队列

//先创建队列  然后 把创建的队列对象加到队列中

NSOperationQueue *queue = [[NSOperationQueuealloc]init];

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

                NSInvocationOperation *operation = [[NSInvocationOperationalloc]initWithTarget:self selector:@selector(run:)object:@"队列线程"];

                [queue addOperation:operation];

           }

2.网络加载的线程

tableView中运用 的网络加载图片
//代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return100;

}

//代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    staticNSString *string = @"cell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:string];

    if (cell ==nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:string];

    }

    _image = cell.imageView;

    cell.imageView.image = [UIImageimageNamed:@"100.jpg"];

    [selfloadImageView:_image];

    return cell;

}

-(void)loadImageView:(UIImageView *)imV{

    [NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:imV];

    

}

-(void)run:(UIImageView *)imgV{

    

    NSLog(@"data == %@",[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]);

    

    //UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];

    UIImage *image = [UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];

    

    [imgV performSelectorOnMainThread:@selector(setImage:)withObject:image waitUntilDone:NO];

    

}

-(void)updateImage:(UIImage *)ima{

    _image.image = ima;

}



3.火车票 线程保护

//线程尽量不要用全局的 释放线程 就必须是陈旭结束了

- (IBAction)open:(UIButton *)sender {

//创建三个窗口卖火车票  

     NSThread *  thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];

    thread1.name = @"1";

    NSLog(@"thread == %@",thread1);

    NSThread *  thread2 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];

    thread2.name = @"2";

    NSThread *  thread3 = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];

    thread3.name = @"3";

    [thread1 start];

    [thread2 start];

    [thread3 start];

}

-(void)run {

       while (true) {

        /*        if (_count > 0) {

            [NSThread sleepForTimeInterval:0.02];

            _count--;

            NSLog(@"卖票窗口:: %@ ,已经卖了一张票,剩余票数::%d",[NSThread currentThread],_count);

        }else{

            NSLog(@"thread == %@",[NSThread currentThread]);

            [NSThread exit];

        }*/

        //卖火车票问题   不允许其他线程访问锁住

      @synchronized(self) {   

          

    if (_count > 0) {

            [NSThread sleepForTimeInterval:0.02];

            _count--;

            NSLog(@"卖票窗口:: %@ ,已经卖了一张票,剩余票数::%d",[NSThread currentThread],_count);

        }else{

            NSLog(@"thread == %@",[NSThread currentThread]);

            [NSThread exit];

        }

    }

    }

}

4.网络加载多线程

//创建一个tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 100;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *string = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];

    }

    _image = cell.imageView;

    cell.imageView.image = [UIImage imageNamed:@"100.jpg"];

    [self loadImageView:_image];

    return cell;

}

-(void)loadImageView:(UIImageView *)imV{

    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:imV];

}

-(void)run:(UIImageView *)imgV{

    NSLog(@"data == %@",[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]);    

    //UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201402/07/20140207151136_JkBcf.jpeg"]]];

    [imgV performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值