ios线程

NSThread实现多线程,初始化线程有:

- init

- initWithTarget:selector:object:

后者可以绑定初始化方法。

 

detachNewThreadSelector可启动一个新的线程。

NSThread创建线程两种方式:

方式一:

[NSThread detachNewThreadSelector:@selector(doSomething_1:) toTarget:self withObject:@"Dwen"];

方式二:

NSThread t1 = [[NSThread alloc] initWithTarget:self selector:@select(doSomething) object:nil];

[t1 start];

 

performSelectorOnMainThread线程结束后,处理绑定的方法。如果程序执行完后需要刷新界面,可通过这种方式进行处理。

ios4如果线程较多,需要手动处理释放线程池:

NSAutoreleasePool *pool;
    for (int i = 0; i < 1000*1000; i++) {
        if(i%1000 == 0)
        {
           pool = [[NSAutoreleasePool alloc] init];
        }
        if(i%1000 == 999)
        [pool release];
        //use s to do something;
        
    }

 ios5以上由于运用了ARC,取消了NSAutoreleasePool类,无需作释放处理。

示例代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (assign,nonatomic) int tickets;
@property (assign,nonatomic) int count;
@property (strong,nonatomic) NSThread *t1;
@property (strong,nonatomic) NSThread *t2;
@property (strong,nonatomic) NSThread *t3;
@property (strong,nonatomic) NSThread *t4;
@property (strong,nonatomic) NSCondition *tc;

- (IBAction)start:(id)sender;

-(void)doSomething;
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tickets;
@synthesize count;
@synthesize t1;
@synthesize t2;
@synthesize t3;
@synthesize t4;
@synthesize tc;

- (void)viewDidLoad
{
    [super viewDidLoad];
	tickets = 10;
    count = 0;
    //锁对象
    tc = [[NSCondition alloc] init];
    t1 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething) object:nil];
    [t1 setName:@"Thread-1"];//线程名称
    [t1 setStackSize:1024*1024];//堆栈大小
    [t1 setThreadPriority:0.8];//线程优先级
    [t1 start];
    
    t2 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething) object:nil];
    [t2 setName:@"Thread-2"];
//    [t1 setStackSize:1024*1024];
//    [t2 setThreadPriority:0.9];
    [t2 start];
    
    t3 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething) object:nil];
    [t3 setName:@"Thread-3"];
    [t1 setStackSize:1024*1024];
    [t3 setThreadPriority:0.6];
    [t3 start];
    
    t4 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething) object:nil];
    [t4 setName:@"Thread-4"];
    [t1 setStackSize:1024*1024];
    [t4 setThreadPriority:1.0];
    [t4 start];
    
    
}

- (IBAction)start:(id)sender {
    [NSThread detachNewThreadSelector:@selector(doSomething_1:) toTarget:self withObject:@"Dwen"];
    [self performSelectorOnMainThread:@selector(doSomething_2:) withObject:@"end thread" waitUntilDone:NO];
}

- (void) doSomething_1 : (NSString *)name{
    NSLog(@"detachNewThreadSelector新启一个线程名叫:%@",name);
}

- (void) doSomething_2 : (NSString *)name{
    NSLog(@"performSelectorOnMainThread调用:%@",name);
}

- (void) doSomething{
    while (TRUE) {
        //上锁
        [tc lock];
        if (tickets > 0) {
            [NSThread sleepForTimeInterval:0.5];
            count = 100 - tickets;
            NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);
            tickets--;
        }else {
            break;
        }
        //解锁
        [tc unlock];
        
    }
}

@end

猜你喜欢

转载自wenxin2009.iteye.com/blog/1702969