多线程初认识

一、多线程的基本概念

二、NSThread

资源竞争原理图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
多条线程同时访问同一个资源
    //4.做耗时操作时,多个线程访问同一个资源
    self.ticketCount = 100;
    self.threadA = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.threadB = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.threadC = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    self.threadA.name = @"售票员A";
    self.threadB.name = @"售票员B";
    self.threadC.name = @"售票员C";
    //启动线程
    [self.threadA start];
    [self.threadB start];
    [self.threadC start];
//执行方法
- (void)saleTicket{
    //锁:必须是全局唯一性的
    //1.注意加锁的位置
    //2.注意加锁的条件,多线程共享同一块资源
    //3.注意加锁是需要付出代价的,需要耗费性能
    //4.加锁的条件:线程同步
    //如果不加锁的话就会出错
        while (1) {
            @synchronized (self) {
            NSInteger count = self.ticketCount;
            if (count > 0) {
                //耗时操作
                for (NSInteger i = 0; i< 100000; i++) {
                }
                self.ticketCount = count -1;
                NSLog(@"%@卖出去了一张票,还剩下%zd张票",  [NSThread currentThread].name,self.ticketCount);
            }else{
                NSLog(@"票卖完了");
                break;
            }
        }
    }
}

 三、GCD

四、NSOperation

五、代码实例

1.NSOperation

 

 

2.GCD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//1.异步函数+串行队列,开一条线程,队列中的任务是串行执行的
dispatch_queue_t queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"download1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download2---%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3---%@",[NSThread currentThread]);
    });
//2.异步函数+并行队列,开多条线程,队列中的任务是并行执行的
dispatch_queue_t queue = dispatch_queue_create("current", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"download1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download2---%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3---%@",[NSThread currentThread]);
    });
//3.异步函数+主队列,所有任务都会在主线程中完成,不会开线程
  dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"download1----%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download2---%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3---%@",[NSThread currentThread]);
    });
//4.同步函数+串行队列,不会开线程,任务是串行执行的
 dispatch_queue_t queue = dispatch_queue_create("seria", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        NSLog(@"downloadload1----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"downloadload2----%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"download3---%@",[NSThread currentThread]);
    });
//5.同步函数+并行队列,不会开线程,任务是并行执行的
  dispatch_queue_t queue = dispatch_queue_create("seria",DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"download01-------%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"download02-------%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"download03-------%@",[NSThread currentThread]);
    });
//6.//同步函数+主队列= 死锁,如果该线程在子线程中执行,那么所有队列就会在主线程执行
    //同步函数,会崩掉
    //同步函数:立刻马上执行,如果我没有执行完毕,那么后面的也别想执行
    //异步函数:如果我没有执行完毕,那么后面的也可以执行
dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"download01------%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"download02------%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"download03------%@",[NSThread currentThread]);
    });
//7.多线程下载图片
//1.创建子线程下载图片
    //DISPATCH_QUEUE_PRIORITY_DEFAULT 0
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //图片地址
        NSURL *url = [NSURL URLWithString:@"http://a.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=da0ec79c738da9774e7a8e2f8561d42f/c83d70cf3bc79f3d6842e09fbaa1cd11738b29f9.jpg"];
        //转换成二进制数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        //转换为图片
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新图片
            self.headImageView.image = image;
        });
    });
//8.延时加载
   //延时加载1
//    [self performSelector:@selector(task) withObject:nil afterDelay:2];
    //延时加载2,用于定时器,repeats是否重复
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(task) userInfo:nil repeats:NO];
    //延时加载3,GCD
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"GCD-----%@",[NSThread currentThread]);
    });
//9.栅栏函数
//栅栏函数不能使用全局并发队列
    //什么是dispatch_barrier_async函数
    //毫无疑问,dispatch_barrier_async函数的作用与barrier的意思相同,在进程管理中起到一个栅栏的作用,它等待所有位于barrier函数之前的操作执行完毕后执行,并且在barrier函数执行之后,barrier函数之后的操作才会得到执行,该函数需要同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用
    //dispatch_barrier_async函数的作用
    //如果是3条线程,并发执行的话,1条线程后面加栅栏函数,必须线程1执行完,才会执行线程2,3
     
    //1.实现高效率的数据库访问和文件访问
    //2.避免数据竞争
//获得全局并发队列
//    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_queue_t queue = dispatch_queue_create("barrier", DISPATCH_QUEUE_CONCURRENT);
    //异步函数
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i++) {
              NSLog(@"download01--%zd------%@",i,[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i++) {
              NSLog(@"download02----%zd----%@",i,[NSThread currentThread]);
        }
    });
    //栅栏函数
    dispatch_barrier_async(queue, ^{
        NSLog(@"==========================");
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i++) {
            NSLog(@"download03--%zd------%@",i,[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i < 100; i++) {
            NSLog(@"download04----%zd----%@",i,[NSThread currentThread]);
        }
    });
//10.GCD的快速迭代
//1.拿到文件的路径
    NSString *fromPath = @"/Users/xingzai/Desktop/Tools";
    //2.获取文件的路径
    NSString *toPath = @"/Users/xingzai/Desktop/Tool";
    //3.得到目录下所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:fromPath];
    //4.count
    NSInteger count = subPaths.count;
    //5.执行
    dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t index) {
       //拼接全路径,文件的路径
        NSString *fullPath = [fromPath stringByAppendingPathComponent:subPaths[index]];
        //拼接全路径,文件剪切到的路径
        NSString *toFullPath = [toPath stringByAppendingPathComponent:subPaths[index]];
         
        //参数一:文件的路径,参数二:文件剪切的位置
        [[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toFullPath error:nil];
    });
//11.创建组队列
 //1.创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //2.创建队列祖
    dispatch_group_t group = dispatch_group_create();
    //3.异步函数
    /**1.封装对象
       2.把任务添加到队列中
       3.会监听执行情况,通知group
     */
    dispatch_group_async(group, queue, ^{
        NSLog(@"1-------%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"2-------%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"3------%@",[NSThread currentThread]);
    });
    //拦截通知,当队列组所有的任务都执行完以后需要执行下面的方法
    dispatch_group_notify(group, queue, ^{
        NSLog(@"线程全部执行完了");
    });
//12.合成对象
/**下载图片1,开子线程
       下载图片2,开子线程
       合成图片,并开子线程
     */
    //1.获取群组
    dispatch_group_t group = dispatch_group_create();
    //2.获取并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //3.下载图片,开启子线程
    dispatch_group_async(group, queue, ^{
        NSLog(@"打印当前线程------%@",[NSThread currentThread]);
        //3.1图片地址
        NSURL *url = [NSURL URLWithString:@"http://www.qbaobei.com/tuku/images/13.jpg"];
        //3.2下载二进制数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        //3.3转换图片
        self.imageOne = [UIImage imageWithData:data];
    });
    //下载图片2
    dispatch_group_async(group, queue, ^{
        NSLog(@"-------%@",[NSThread currentThread]);
        //1.图片地址
        NSURL *url = [NSURL URLWithString:@"http://pic1a.nipic.com/2008-09-19/2008919134941443_2.jpg"];
        //2.下载二进制数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        //3.转换图片
        self.imageTwo = [UIImage imageWithData:data];
    });
    //合成图片
    dispatch_group_notify(group, queue, ^{
         
        NSLog(@"-----------%@",[NSThread currentThread]);
        //1.创建图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(320, 640));
        //2.画图形
        [self.imageOne drawInRect:CGRectMake(0, 0, 320, 320)];
        self.imageOne = nil;
         
        [self.imageTwo drawInRect:CGRectMake(0, 320, 320, 320)];
        self.imageTwo = nil;
        //3.根据上下文得到一张图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        //4.关闭上下文
        UIGraphicsEndImageContext();
         
        NSLog(@"%@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI
            self.headImageView.image = image;
        });
    });
    //由于dispatch_apply函数与dispatch_sync函数相同,会等待处理执行结束,因此推荐在dispatch_async函数中非同步地执行dispatch_apply函数
posted @   TheYouth  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示