iOS NSThread线程

 声明

int tickets;
    int count;
    NSThread* ticketsThreadone;
    NSThread* ticketsThreadtwo;
    NSCondition* ticketsCondition;
    NSLock *theLock;

实现 创建线程

 tickets = 100;
    count = 0;
    theLock = [[NSLock alloc] init];
    // 锁对象
    ticketsCondition = [[NSCondition alloc] init];
    ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadone setName:@"Thread-1"];
    [ticketsThreadone start];
    
    ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadtwo setName:@"Thread-2"];
    [ticketsThreadtwo start];
    
    NSThread *ticketsThreadthree = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [ticketsThreadthree setName:@"Thread-3"];
    [ticketsThreadthree start];

run方法

- (void)run{
    while (TRUE) {
//        // 上锁
//        [ticketsCondition lock];
//        [theLock lock];//锁住
//        if(tickets >= 0){
//            [NSThread sleepForTimeInterval:0.09];
//            count = 100 - tickets;
//            NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
//            tickets--;
//        }else{
//            break;
//        }
//        [theLock unlock];//开锁
//        [ticketsCondition unlock];
        
        //线程锁  (简化)
        @synchronized(self) {
            if(tickets >= 0){
                [NSThread sleepForTimeInterval:0.09];
                count = 100 - tickets;
                NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
                tickets--;
            }else{
                break;
            }
        }//end @synchronized
    }//end while
}

例子:线程下载图片

//
//  ViewController.m
//  NSThreadDemo
//
//  Created by 屎壳郎情调 on 13-9-23.
//  Copyright (c) 2013年 ibokan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
/*线程间通讯
线程下载完图片后怎么通知主线程更新界面呢?
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
performSelectorOnMainThread是NSObject的方法,除了可以更新主线程的数据外,还可以更新其他线程的比如:
用:performSelector:onThread:withObject:waitUntilDone:
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    imageView1  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 210, 320, 300)];
    [self.view addSubview:imageView];
    [imageView release];
    [self.view addSubview:imageView1];
    [imageView1 release];
    [NSThread detachNewThreadSelector:@selector(downImage1) toTarget:self withObject:nil];
    //线程
    [NSThread detachNewThreadSelector:@selector(downImage) toTarget:self withObject:nil];
    //(等效)
	//NSThread *th = [[NSThread alloc] initWithTarget:self selector:@selector(downImage) object:nil];
    //[th start];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//下载图片
-(void)downImage
{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://aa540933120.host.yzdns.cn/images/baidu.jpg"]];
    UIImage *image = [UIImage imageWithData:data];
   if(image!=nil)
   {
       //跟新主线程(线程的通信)
       [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
   }
}
//下载图片
-(void)downImage1
{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.5wants.cc/WEB/File/U3325P704T93D1661F3923DT20090612155225.jpg"]];
    UIImage *image = [UIImage imageWithData:data];
    if(image!=nil)
    {
        //跟新主线程(线程的通信)
        [self performSelectorOnMainThread:@selector(updateUI1:) withObject:image waitUntilDone:YES];
    }
}
//刷新页面UI
-(void)updateUI:(UIImage *)image
{
    imageView.image = image;
}
//刷新页面UI
-(void)updateUI1:(UIImage *)image
{
    imageView1.image = image;
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值