zoukankan      html  css  js  c++  java
  • 多线程技术

    多线程: 串行->针对一个线程中有多个任务,按顺序执行。

       并行->多个线程的执行情况,同时执行。

    多线程优点: 提高程序的性能

    缺点:  需要开销,程序更加复杂。

     

    多线程技术方案: pthread 适用于unix, Linux, Windows,可跨平台

    NSThread 面向对象,简单易用

    GCD 充分利用设备的多核,旨在替代NSThread等线程技术

    NSOperation 基于GCD底层是 GCD,比GCD 多了一些简单实用的功能。

     

    pthread使用:

     

    //创建线程

    pthread_t thread = nil;

    pthread_create(&thread, NULL, run, NULL);

     

     

    void * _Nullable run(void * _Nullable param){

     

    //执行耗时操作,放在这个方法

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

        

        return NULL;

    }

     

    NSThread 使用:(创建线程的几种方法)

     1, 

         NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(runOnBackgroundThread) object:nil];

        [thread start];

    2,

    [NSThread detachNewThreadWithBlock:^{

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

            NSLog(@"耗时操作");

        }];

    3,

    [NSThread detachNewThreadSelector:@selector(runOnBackgroundThread) toTarget:self withObject:nil];

     

    4,

    [self performSelectorInBackground:@selector(runOnBackgroundThread) withObject:nil];

     

    设置线程的属性:

     

         thread.threadPriority = 1.0;  //设置线程的优先级,  从0到1, 优先级越高, 被CPU调到的概率越大。

        thread.name = @"线程1"; //设置线程的名称

    线程的生命周期 : 当线程内任务执行完毕后会被释放。

  • 相关阅读:
    「ruby/MiniMagick」用MiniMagick处理图片
    「thunar」给thunar增加搜索文件功能
    Software--Architecture--SOA Factory
    DataArchitecture--数据结构与算法 (Java)
    Software--Architecture--SOA 面向服务体系结构
    Software--Develop -- WCF Setting
    Industry--OPC UA (OPC Unified Architecture) 统一架构
    Software--IoC 依赖倒置 控制反转
    虚拟表dual。字符串函数UPPER,LOWER。&变量。INITCAP,LENGTH,SUBSTR
    运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。
  • 原文地址:https://www.cnblogs.com/dashengios/p/10370827.html
Copyright © 2011-2022 走看看