zoukankan      html  css  js  c++  java
  • 多线程 总结一

    一、NSThread
    1、创建和启动3种方式
    1>先创建,后启动
    //创建
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:@"http://XXX"];
    //启动
    [thread start];

    2>创建完全自动启动
    [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://XXX"];

    3>隐式创建(自动启动)
    [self performSelectorInBackground:@selector(download:) withObject:@"http://XXX"];

    2.常用方法
    1>获取当前线程
    + (NSThread *)currentThread;

    2>获取主线程
    + (NSThread *)mainThread;

    3>睡眠(暂停)线程
    + (void)sleepUntilDate:(NSDate *)date;
    + (void)sleepForTimeInterval:(NSTimeInterval)ti;

    4> 设置线程的名字
    - (void)setName:(NSString *)n;
    - (NSString *)name;

    二、线程同步
    1、为防止多个线程抢夺同一资源的数据安全问题

    2、实现:为代码加一互斥锁(同步锁)
    @synchronized(self) {
    //被锁代码
    }

  • 相关阅读:
    C++之类和对象
    PHP程序设计基础
    PHP函数和MySQL数据库
    HTML语言基础
    文件和目录1(文件属性和权限)
    文件IO
    查找
    使用tcpdump抓包实例
    导入模块的2种方法
    ansible启用sudo执行命令
  • 原文地址:https://www.cnblogs.com/fangchun/p/4684912.html
Copyright © 2011-2022 走看看