zoukankan      html  css  js  c++  java
  • 多线程Demo1 了解

     
    首先演示一下主线程的阻塞
     
    //  DYFViewController.m
    //  623-01-阻塞多线程
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
     
    #import "DYFViewController.h"
     
    @interface DYFViewController ()
     
    @end
     
    @implementation DYFViewController
    - (IBAction)btnOnClick {
        // 1.获取当前的线程
        NSThread *thread = [NSThread currentThread];
         
        // 2.打印线程
        NSLog(@"%@", thread);
         
        // 3.执行一线耗时的操作
        for (int i = 0; i < 9999; i++) {
            NSLog(@"%@", thread);
            // 此时点击按钮,在执行完耗时操作之前,按钮一直是高亮状态,期间用户点击其他的UI控件也不好响应
        }
    }
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
     
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
     

    @end

    容易发现,在耗时操作期间,其它UI操作都被延时了,造成用户的卡顿现象

    --------创建子线程方法1--pthread

    //  DYFViewController.m
    //  623-02-pthread
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
     
    #import "DYFViewController.h"
    #import <pthread.h>
     
    @interface DYFViewController ()
     
    @end
     
    @implementation DYFViewController
     
    // c语言函数
    void *run(void *data)
    {
        // 1.获取当前的线程
        NSThread *cThread = [NSThread currentThread];
         
        // 2.打印线程
        NSLog(@"%@", cThread);
         
        // 3.h耗时操作
        for (int i = 0; i < 9999; i++) {
            NSLog(@"%@", cThread);
        }
     
        return NULL;
    }
     
    - (IBAction)btnOnClick {
        // 1.获取当前的线程
        NSThread *thread = [NSThread currentThread];
         
        // 2.打印线程
        NSLog(@"%@", thread);
         
        // 3.执行一线耗时的操作 : 创建一套子线程
         
        pthread_t threadId;
        pthread_create(&threadId, NULL, *run, NULL);
        
    }
     
     

    @end

     
     
  • 相关阅读:
    代码演示C#各版本新功能
    【转】Spring Security Authentication (认证)
    maven groupId分组名称,artifactId项目名称
    【转】Maven的本地仓库和镜像源配置
    【转】asp.net core环境变量详解
    【转】建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板
    【转】VS中添加自定义代码片段
    【转】Java JDK和IntelliJ IDEA 配置及安装
    Download .NET Core
    站点部署,IIS配置优化指南[转]
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4664135.html
Copyright © 2011-2022 走看看