zoukankan      html  css  js  c++  java
  • 杀死一个线程

    ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run1) object:nil];
    [ticketsThreadone setName:@"Thread-1"];
    [ticketsThreadone start];
    
    ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run2) object:nil];
    [ticketsThreadtwo setName:@"Thread-2"];
    [ticketsThreadtwo start];
    
    
    - (void)run1{
    while (YES) {
    [NSThread sleepForTimeInterval:1];
    NSLog(@"当前数是:%d,线程名:%@",count,[[NSThread currentThread] name]);
    count++;
    if (count == 10) {
    [ticketsThreadtwo cancel];
    }
    }
    }
    - (void)run2{
    while (YES) {
    if ([[NSThread currentThread] isCancelled]) {
    [NSThread exit];
    }
    [NSThread sleepForTimeInterval:1];
    NSLog(@"当前数是:%d,线程名:%@",count,[[NSThread currentThread] name]);
    count++;
    }
    }
    
    如果有什么不明白再交流

    感想: while(true)  + if...else 组合 其实就相当于 for  循环。  所以说,for 循环可以被拆分成while   if  用于 多线程。

    - (void)run{  
        while (TRUE) {  
            // 上锁  
            [ticketsCondition lock];  
            [ticketsCondition wait];  
            [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];  
        }  
  • 相关阅读:
    包含min函数的栈
    量化交易系统的四个组成部分
    顺时针打印矩阵
    python正则表达式中re.M,re.S,re.I的作用
    二叉树的镜像
    树的子结构
    合并两个排序的链表
    反转链表
    命名元祖
    二叉树(二叉搜索树-AVL树-B树)
  • 原文地址:https://www.cnblogs.com/ygm900/p/3102761.html
Copyright © 2011-2022 走看看