几个概念:

进程:“正在运行”应用程序(app)就是一个进程,它至少包含一个线程;
           进程的作用:为应用程序开辟内存空间;
线程:CPU调度的最小单元;
          线程的作用:执行app的代码;

进程和应用程序的关系:进程为应用程序开辟内存空间;
线程和应用程序的关系:线程执行应用程序的代码;
进程和线程之间的关系:进程是由线程组成的,一个进程中至少有一个线程;

iOS中开启线程
方法一:——C语言方式
     pthread 
步骤:
             导入头文件:#import<pthread.h>
      创建一个子线程:
            pthread_create(<#pthread_t *restrict#>, <#const pthread_attr_t *restrict#>, <#void *(*)(void *)#>, <#void *restrict#>)
           参数1:<#pthread_t *restrict#> 线程标识符的地址;
           参数2:线程的属性;
           参数3:函数指针(指向子进程中需要执行的函数);
           参数4:传递给子进程的参数; 
           
           pthread_tmyPthread;

           
NSString
*str  =
@"我是一个华丽的子线程";
           
pthread_create(&myPthread,
NULL,
longTimeOperation, (__bridgevoid*)(str));
Source code:

          #import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
pthread_t myPthread;
NSString *str = @"我是一个华丽的子线程";
pthread_create(&myPthread, NULL, longTimeOperation, (__bridge void*)(str));
}
void *longTimeOperation(void * data){
for (int i = 0; i < 10000; i++) {
NSLog(@"%s : %d %@ %@",__func__,i,[NSThread currentThread],data);
}
return NULL;
}

 方法二:——OC方式
          利用 NSthread类
          步骤:
                  实例化对象:
- (instancetype)initWithTarget:(id)target
selector:(SEL)selector object:(nullableid)argument
                  开启线程:   [thread start];
                  实现线程方法:

          进程属性:    
           (1)线程名称:(方便调试,主线程和子线程名称可以一样);
           (2)线程内存大小 :(栈区)
                         iOS7之前,默认主1M,子512K;
                         现在,默认都是1M;
           (3)线程优先级(慎用);

Source code:

            #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSThread *mythread = [[NSThread alloc] initWithTarget:self selector:@selector(longTimeOperation:) object:@"我是华丽的子进程"];
mythread.name = @"longTime";
[mythread start];
}
- (void)longTimeOperation:(NSString *)parameter { for (int i = 0; i < 10000; i++) {
NSLog(@"%s : %d %@ %@",__func__,i,[NSThread currentThread],parameter); if (i == 9999) {
NSLog(@"退出进程");
[NSThread exit];
}
}
}

除此以外:
                         还有两种开启进程的方法:
                          1、自动从当前线程分离出新线程NSThread的类方法
               + (void)detachNewThreadSelector:(SEL)selector
toTarget:(id)target withObject:(nullableid)argument;
                          2、隐式的开启线程的方法(NSObject的分类)
                              - (void)performSelectorInBackground:(SEL)aSelector
withObject:(nullableid)arg

常用方法:
    名字/获得主线程/获得当前线程/阻塞线程/退出线程

   
//
不常用:栈区大小/优先级

   
1>获得当前线程
    + (NSThread *)currentThread;
   2>获得主线程
    + (NSThread *)mainThread;
   3>睡眠(暂停)线程

    + (void)sleepUntilDate:(NSDate *)date;
    + (void)sleepForTimeInterval:(NSTimeInterval)ti;
   4>设置线程的名字

    - (void)setName:(NSString *)n;
    - (NSString *)name;



线程的状态



当然除了以上两种方式,还有GCD和NSOperation,此文暂时不做分析;



线程同步技术:

可以解决多线程资源共享/竞争
     利用锁技术来解决:
     互斥锁:安静的等待
            方式1、
                 
   
NSLock
*lock = [[NSLockalloc]init];
            //
加锁
              [locklock];
              //中间就是锁住的内容
             //

解锁

              [lockunlock];
           方式二、
                    //唯一对象self
                   @synchronized(self){
                         
                    }
     原子锁:不停的敲门

     原子锁 与互斥suo的区别:
           一写多读,只为set方法加锁!不管读;
           互斥锁则是读写都保护;

内存空间:
          公共区域:每一个属性和对象都可以访问;
          私人区域:需要加锁;
          有限的公共区域:不能同时允许多个对象/属性访问;
          为了解决“有限的公共区域:不能同时允许多个对象”的问题,信号量就诞生了!
          信号量:互斥锁就是信号量为1的特殊情况;

进程间通信小案例:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[NSThread detachNewThreadSelector:@selector(downLoadImage) toTarget:self withObject:nil];
//NSLog(@"%s : %@",__func__,[NSThread currentThread]);
}
- (void)downLoadImage {
NSLog(@"%s : %@",__func__,[NSThread currentThread]);
NSString *urlStr = @"http://img5.duitang.com/uploads/item/201408/18/20140818224957_2tkUd.jpeg";
// urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data]; [self performSelectorOnMainThread:@selector(downLoadImageView:) withObject:image waitUntilDone:YES];
//NSLog(@"------");
}
- (void)downLoadImageView:(UIImage *)image {
//NSLog(@"%s : %@",__func__,[NSThread currentThread]);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
imageView.image = image;
[self.view addSubview:imageView];
} @end

进程间通信案例总结:
 1、iOS9网络适配
                 iOS9把所有之前的http请求全部改成了https请求。统一采用TLS 1.2 SSL。
         解决方法分为两大类:
           (1)服务器进行更更新。
               据了解。这个应该是和服务器是什么系统有关。对应的解决方式也不同。应该是需要对症下药。
            (2)移动端回退到之前相对来说不安全的http。
     此时需要对Info.plist进行修改。
     这里面又分为两大类:
     指定某些请求使用之前的方式;
     让所有的请求使用之前的方式。

指定某些请求使用之前的方式
Source code:
<key>NSAppTransportSecurity</key>

<dict>
<key>NSExceptionDomains</key>
<dict>
<key>www.baidu.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
NSExceptionDomains:里面放的是允许使用http请求的sever字典。
www.baidu.com:允许使用http请求的sever的名字,里面放的是对这个sever的一切配置。
NSIncludesSubdomains:是否允许子域名。
NSTemporaryExceptionAllowsInsecureHTTPLoads:是否允许进行http请求。






让所有的请求使用之前的方式。


1 直接修改:info.plist




2 代码方式修改:open source Code  info.plist文件:





Source code:
     <key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
     移动端而言,第一种方式相比第二种方式更加合理。第二种方式简单粗暴。如果偷懒,那自然是使用第二种方式。
参考:

2、百分号转义
URL:不能出现汉字/空格/特殊字符!
此时需要百分号转义

iOS9.0之前:
    urlStr = [urlStr

stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


iOS9.0之后:
    urlStr = [urlStr

stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet

URLQueryAllowedCharacterSet]];

    



iOS-pthread && NSThread && iOS9网络适配的更多相关文章

  1. iOS9网络适配(ATS)

    [转]iOS9 new_001:iOS9网络适配(ATS) 下载Xcode7打开APP后大家都发现自己的APP无法联网了,why? 苹果官方文档介绍如下: App Transport Security ...

  2. iOS9网络适配

    今天升级Xcode7.0,发现网络访问失败. 输出错误信息:The resource could not be loaded because the App Transport Security po ...

  3. iOS多线程 NSThread/GCD/NSOperationQueue

    无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...

  4. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)

    继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...

  5. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)(转载)

    继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...

  6. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  7. 李洪强iOS下的实际网络连接状态检测

    iOS下的实际网络连接状态检测 序言 网络连接状态检测对于我们的iOS app开发来说是一个非常通用的需求.为了更好的用户体验,我们会在无网络时展现本地或者缓存的内容,并对用户进行合适的提示.对绝大部 ...

  8. 多线程之pthread, NSThread, NSOperation, GCD

    关于多线程会有一系列如下:多线程之概念解析 多线程之pthread, NSThread, NSOperation, GCD 多线程之NSThread 多线程之NSOperation 多线程之GCD p ...

  9. ios9网络请求https适配

    发现问题:今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息: The resource could not be loaded because the App Transport S ...

随机推荐

  1. ClickHouse基本操作(二)

    一.先来说一下,ClickHouse为啥快 MySQL单条SQL是单线程的,只能跑满一个core,ClickHouse相反,有多少CPU,吃多少资源,所以飞快: ClickHouse不支持事务,不存在 ...

  2. 实现es6中的set和map

    转载自: https://www.cnblogs.com/hui-fly/p/9459152.html https://blog.csdn.net/roamingcode/article/detail ...

  3. excel导入DataTable

    http://www.cnblogs.com/top5/archive/2010/03/12/1684559.html --下载excel的dll http://bbs.csdn.net/topics ...

  4. springboot中yml常用配置

    server: port: 8080 spring: datasource: #数据源配置 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc: ...

  5. linux静态网络设置

    一:NET模式 第一种: 第二种方式: 三:重启服务

  6. akka-typed(0) - typed-actor, typed messages

    akka 2.6.x正式发布以来已经有好一段时间了.核心变化是typed-actor的正式启用,当然persistence,cluster等模块也有较大变化.一开始从名称估摸就是把传统any类型的消息 ...

  7. Spark SQL源码解析(五)SparkPlan准备和执行阶段

    Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Spark SQL源码解析(三 ...

  8. 【JAVA习题七】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

    package erase; import java.util.Scanner; public class 字符串分类 { public static void main(String[] args) ...

  9. [JavaWeb基础] 031.dom4j写入xml的方法

    上一篇我们讲述了dom4j读取xml的4种方法,甚是精彩,那么怎么样写入xml呢?我们直接看下源码实现. public static void main(String[] args) throws E ...

  10. Spring_基于配置文件的方式配置AOP

    applicationContext-xml.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...