美文网首页
iOS 线程

iOS 线程

作者: 一直很安静_25ae | 来源:发表于2019-07-14 11:11 被阅读0次

pthread

#import "ViewController.h"
#import <pthread.h>//需要导入头文件
@interface ViewController ()
@end

@implementation ViewController
void * run(void *param)
{
    for (NSInteger i = 0; i<50000; i++) {
        NSLog(@"------buttonClick---%zd--%@", i, [NSThread currentThread]);
    }
    return NULL;
}

- (IBAction)buttonClick:(id)sender {
    pthread_t thread;
    pthread_create(&thread, NULL, run, NULL);
    
    pthread_t thread2;
    pthread_create(&thread2, NULL, run, NULL);
}

@end

NSThread

  • 第一种创建方式
- (void)createThread1
{
    //    // 创建线程
    //    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"jack"];
    //    // 启动线程
    //    [thread start];
    // 创建线程
    XMGThread *thread = [[XMGThread alloc] initWithTarget:self selector:@selector(run:) object:@"jack"];
    thread.name = @"my-thread";
    // 启动线程
    [thread start];
}

- (void)run:(NSString *)param
{
    for (NSInteger i = 0; i<100; i++) {
        NSLog(@"-----run-----%@--%@", param, [NSThread currentThread]);
    }
}
  • 第二种创建方式
- (void)createThread2
{
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"rose"];
}
//这种方式创建的线程不需要我们手动开启
- (void)run:(NSString *)param
{
    for (NSInteger i = 0; i<100; i++) {
        NSLog(@"-----run-----%@--%@", param, [NSThread currentThread]);
    }
}
  • 第三种创建线程的方式
- (void)createThread3
{
    [self performSelectorInBackground:@selector(run:) withObject:@"jack"];
}
- (void)run:(NSString *)param
{
    for (NSInteger i = 0; i<100; i++) {
        NSLog(@"-----run-----%@--%@", param, [NSThread currentThread]);
    }
}

NSThread线程的状态

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
}

- (void)run
{
    for (NSInteger i = 0; i<100; i++) {
        NSLog(@"-----%zd", i);
        
        if (i == 49) {
            [NSThread exit]; // 直接退出线程
        }
    }
}

- (void)run2
{
    NSLog(@"-------");
//    [NSThread sleepForTimeInterval:2]; // 让线程睡眠2秒(阻塞2秒)
//    [NSThread sleepUntilDate:[NSDate distantFuture]];
    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
    NSLog(@"-------");
}

@end

NSThread线程安全问题

#import "ViewController.h"

@interface ViewController ()
/** 售票员01 */
@property (nonatomic, strong) NSThread *thread01;
/** 售票员02 */
@property (nonatomic, strong) NSThread *thread02;
/** 售票员03 */
@property (nonatomic, strong) NSThread *thread03;

/** 票的总数 */
@property (nonatomic, assign) NSInteger ticketCount;

/** 锁对象 */
//@property (nonatomic, strong) NSObject *locker;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.locker = [[NSObject alloc] init];
    
    self.ticketCount = 100;
    
    self.thread01 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread01.name = @"售票员01";
    
    self.thread02 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread02.name = @"售票员02";
    
    self.thread03 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.thread03.name = @"售票员03";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.thread01 start];
    [self.thread02 start];
    [self.thread03 start];
}

- (void)saleTicket
{
    while (1) {
        @synchronized(self) {
            // 先取出总数
            NSInteger count = self.ticketCount;
            if (count > 0) {
                self.ticketCount = count - 1;
                NSLog(@"%@卖了一张票,还剩下%zd张", [NSThread currentThread].name, self.ticketCount);
            } else {
                NSLog(@"票已经卖完了");
                break;
            }
        }
    }
}

@end

相关文章

网友评论

      本文标题:iOS 线程

      本文链接:https://www.haomeiwen.com/subject/aadbkctx.html