OBjective-c NSThread 多線程1




#import "ViewController.h"c++

#define ROW 5網絡

#define COLUMN 3ide

#define IMAGR_COUNT ROW*COLUMNspa

#define W 100.net

#define H W線程


@interface ViewController ()3d

{orm

    NSMutableArray *muarray;進程

    NSArray * arrayWANGZHI;圖片

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    arrayWANGZHI=@[

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://img.tuku.cn/file_big/201409/048f4c825de5442e952ddc5e28563100.jpg",

                             @"http://p7.qhimg.com/t01308022902bb3cc15.jpg",

                             @"http://www.33lc.com/article/UploadPic/2012-8/2012815167420064.jpg",

                             @"http://d.3987.com/jmfghjgqbz.120925/004.jpg",

                             

                             @"http://img.bbs.duba.net/month_1011/1011020907c8b129640ca5a7e7.jpg",

                             

                             @"http://www.deskcar.com/desktop/car/201337162241/6.jpg",

                             @"http://www.deskcar.com/desktop/car/201337162241/6.jpg",

                             @"http://pic11.nipic.com/20101117/778850_171649021175_2.jpg",

                             @"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",

                             

                             @"http://kuoo8.com/wall_up/hsf2288/200911/200911031144096643.jpg",

                             @"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",

                             

                             @"http://www.deskcar.com/desktop/car/2005/2007121201442/10.jpg"

                             

                             ];


    

    

    [self layout];

    

}

-(void)layout{

    muarray=[[NSMutableArray alloc]initWithCapacity:10];

    for (int r=0; r<COLUMN; r++) {

        for (int c=0; c<ROW; c++) {

            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+r*118.75, 20+c*118.75, W, H)];

            [self.view addSubview:imageView];

            imageView.backgroundColor=[UIColor orangeColor];

            [muarray addObject:imageView];

            

        }

    }

    UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(150, 600, 100, 20)];

    [button setTitle:@"加載圖片" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    

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

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

    [self.view addSubview:button];

    

    

    

}

-(void)updateImage:(NSArray*)array{

    UIImage * image=[UIImage imageWithData:array[0]];

    UIImageView * imageViewI=muarray[[array[1] intValue]];

    imageViewI.image=image;

    

    

    

}

-(void)gengxinImage:(NSNumber*)index{

    NSLog(@"%@",[NSThread currentThread].name);

    //讓最後一個線程優先執行,讓其餘線程延遲兩秒

    if (![index isEqual:@14]) {

        [NSThread sleepForTimeInterval:2];

    }

    NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:arrayWANGZHI[[index intValue]]]];

    NSLog(@"%@", arrayWANGZHI[[index intValue]]);

    NSArray * dataArray=@[data,index];

    

    [self performSelectorOnMainThread:@selector(updateImage:) withObject:dataArray waitUntilDone:YES];

   

    

    

}

-(void)xiazaiXianCheng{

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

        NSThread * thread=[[NSThread alloc]initWithTarget:self selector:@selector(gengxinImage:) object:[NSNumber numberWithInt:i]];

        thread.name=[NSString stringWithFormat:@"個人線程 %d",i];

        [thread start];//啓動線程

        

    }

}


#pragma mark 改變線程優先級

//提升他被優先加載的概率,可是未必就第一個加載。1其餘進程是先啓動的2網絡情況咱們沒有辦法修改

-(void)sueMultiThread{

    NSMutableArray * muarray2=[[NSMutableArray alloc]initWithCapacity:10];

    

    for (int a=0; a<IMAGR_COUNT; a++) {

        NSThread * thread2=[[NSThread alloc]initWithTarget:self selector:@selector(gengxinImage:) object:[NSNumber numberWithInt:a]];

        if (a==IMAGR_COUNT-1) {

            thread2.threadPriority=1.0;

            

        }else{

            thread2.threadPriority=0;

            

        }

        

        [muarray2 addObject:thread2];

    }

    

    for (int s=0; s<IMAGR_COUNT; s++) {

        NSThread * thread3=muarray2[s];

        [thread3 start];

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    

  

    

}


@end

相關文章
相關標籤/搜索