MyIOS开发篇 -- 多线程是什么

首先分析多线程的使用环境:

多线程处理包括Core Data的多线程访问,UI的并行绘制,异步网络请求以及一些在运行态内存吃紧的情况下处理大文件的方案等。

其次,分别举例说明iOS提供的多线程的实现方法

iOS中提供了以下集中中多线程的实现方式

1.NSOBjcet实现

?
1
2
3
4
// 最简单的多线程 执行方式
     // 参数1:需要在后台(子线程)执行方法
     // 参数2:给这个方法传参
     [self performSelectorInBackground:@selector(btnUpClicked:)withObject:nil];

2.NSThread实现

?
1
2
3
4
5
6
7
// 优点:在所有的多线程实现方式中  最轻量级
//      可以按照需求 任意控制thread对象 即:线程的加锁等操作
// 缺点:控制太繁琐 不能自己控制线程安全
     // NSThread 的一个对象 就代表一个线程
     NSThread * thread  = [[NSThread alloc]initWithTarget:selfselector:@selector(btnUpClicked:)object:nil];
     [ thread  start];
     [ thread  release];

3.NSOperation实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// NSOperation代表一个任务 自己不能实现多线程
// NSOperation是一个抽象类 不能直接使用 需要创建一个子类去编写实现的内容
// 任务开始 会在当前线程执行任务(main方法中的代码)
 
     MyOperation *operation = [[MyOperation alloc] init];
     [operation start];
     [operation release];
     
     // 系统已经写好了两个类 可以直接使用
//    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#>]
     NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
             // 填写你要执行的任务内容
     }];
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@implementation MyOperation
 
- ( void )main
{
     // operation在main方法中写要执行的任务
     
     static  int  count = 0 ;
     
     for  ( int  i = 0 ; i < 600000000; i ++) {
         count ++;
     }
     NSLog(@ "%d" , count);
     
     // 判断当前任务是否为主线程  0为不是 1为是
     BOOL  b = [NSThread isMainThread];
     NSLog(@ "%d" , b);
     
     NSThread * thread  = [NSThread currentThread];
     NSLog(@ "当前线程为:%@" thread );
     
}
@end

4.NSOperationQueue

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     // 任务队列(NSOperationQueue) 内部管理一些列的线程
     
     MyOperation *op1 = [[MyOperation alloc] init];
     MyOperation *op2 = [[MyOperation alloc] init];
     MyOperation *op3 = [[MyOperation alloc] init];
     MyOperation *op4 = [[MyOperation alloc] init];
     
     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
     
     // 设置最大并发数
     [queue setMaxConcurrentOperationCount:2];
     
     [queue addOperation:op1];
     [queue addOperation:op2];
     [queue addOperation:op3];
     [queue addOperation:op4];
     
     // 优点:能够自己管理 线程安全 能够合理的安排线程 降低系统的开销 提高对线程的利用率
     // 缺点:可读性差 写起来比较繁琐

5.GCD

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     // GCD 是基于队列的 多线程实现方式
     // 队列的类型
     // 1.并行: 多个任务同时执行 Concurrent
     // 2.串行: 依次执行任务(同一事件只执行一个任务) Serial
     
     // 自己创建一个队列
     // 参数1:给队列起个名字
     // 参数2:选择队列的类型 DISPATCH_QUEUE_CONCURRENT(串行) 
     dispatch_queue_attr_t queue = dispatch_queue_create( "test" , DISPATCH_QUEUE_CONCURRENT);
     
     // 使用队列
     // 参数1: 在哪个队列执行代码
     // 参数2: 要执行的代码
     dispatch_async(queue, ^{
         [self btnUpClicked:nil];
     });
高级用法:
?
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
     // 系统定义了5个队列
     // 1. 一个串行队列  主队列作用 : 管理主线程的相关事务
     
     // 获得主队列
     dispatch_queue_t mainQueue = dispatch_get_main_queue();
     // 在主队列执行任务
//    dispatch_async(mainQueue, ^{
//        [self btnUpClicked:nil];
//    });
     
     
     // 2. 4个并行队列 (并行队列)
     // 参数1: 选择队列的优先级
     // 参数2: 给未来使用 一般填0
     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_async(globalQueue, ^{
//        [self btnUpClicked:nil];
//    });
     
     // 高端使用
     // 1.全局队列获取数据 回到主队列将数据加载到视图上
//    dispatch_async(globalQueue, ^{
//       
//        // 全局队列(子线程)获取数据
//        NSString *str = @"htt://www.baidu.com";
//        NSURL *url = [NSURL URLWithString:str];
//        
//        NSData *data = [NSData dataWithContentsOfURL:url];
//        UIImage *image = [UIImage imageWithData:data];
//        
//        // 返回主线程 加载UI/更新UI
//        dispatch_async(mainQueue, ^{
//           
//            UIImageView *imageView = [[UIImageView alloc] init];
//            // 给视图赋值
//            imageView.image = image;
//            
//        });
//    });
     
     // 2.一段代码只执行依次
     // 大部分的时候 都在单例方法中使用
     static  dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         NSLog(@ "只执行依次" );
     });
     
     // 3.一段代码执行多次 (全局队列有效)
     // 参数1: 执行多少次
     // 参数2: 在哪个队列有效
     // 参数3: 要执行的内容
     dispatch_apply(4, globalQueue, ^( size_t  a) {
         NSLog(@ "%zu" , a);
     });
     
     // 4.定时
     // 参数1: 间隔几秒
     // 参数2: 执行的内容
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         NSLog(@ "=====" );
     });


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!很高兴回答你关于Python游戏脚本入门的问题。在Python中,多线程是一种并发处理的技术,它允许程序同时执行多个线程,从而提高程序的运行效率和响应能力。在游戏开发中,多线程可以用于处理游戏中的多个任务或实现并行计算。 要在Python中使用多线程,可以使用内置的`threading`模块。下面是一个简单的示例,演示如何在Python中创建和启动多个线程: ```python import threading def task(): # 这里是线程要执行的任务 print("Hello, I'm running in a thread!") # 创建线程对象 thread1 = threading.Thread(target=task) thread2 = threading.Thread(target=task) # 启动线程 thread1.start() thread2.start() ``` 在上面的示例中,我们首先定义了一个`task`函数,这是线程要执行的具体任务。然后,我们使用`threading.Thread`类创建了两个线程对象`thread1`和`thread2`,并将`task`函数作为参数传递给它们。最后,我们调用`start`方法来启动这两个线程。 多线程的执行是并发的,所以你可能会看到输出信息交替出现。在实际的游戏开发中,你可以利用多线程来处理不同的游戏逻辑、计算复杂的物理模拟或者处理网络通信等任务,从而提升游戏的性能和玩家体验。 但是需要注意的是,多线程编程需要注意线程之间的同步和资源竞争问题。在游戏开发中,你可能需要使用锁和同步原语来确保线程之间的安全操作。 希望这个简单的介绍对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值