Swift - 多线程实现方式(1) - 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
|
import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //方式1:使用类方法 NSThread.detachNewThreadSelector("downloadImage", toTarget: self, withObject: nil) //方式2:实例方法-便利构造器 var myThread:NSThread = NSThread(target: self, selector: "downloadImage", object: nil) myThread.start() } //定义一个下载图片的方法,线程调用 func downloadImage(){ var data = NSData(contentsOfURL: NSURL(string: imageUrl)!, options: nil, error: nil) println(data?.length) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }} |
4,线程同步
|
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
|
import UIKitclass ViewController: UIViewController { //定义两个线程 var thread1:NSThread? var thread2:NSThread? //定义两个线程条件,用于锁住线程 let condition1 = NSCondition() let condition2 = NSCondition() override func viewDidLoad() { super.viewDidLoad() thread2 = NSThread(target: self, selector: "method2:", object: nil) thread1 = NSThread(target: self, selector: "method1:", object: nil) thread1?.start() } //定义两方法,用于两个线程调用 func method1(sender:AnyObject){ for var i=0; i<10; i++ { print("NSThread 1 running \(i)") sleep(1) if i == 2 { thread2?.start() //启动线程2 //本线程(thread1)锁定 condition1.lock() condition1.wait() condition1.unlock() } } print("NSThread 1 over") //线程2激活 condition2.signal() } //方法2 func method2(sender:AnyObject){ for var i=0; i<10; i++ { print("NSThread 2 running \(i)") sleep(1) if i == 2 { //线程1激活 condition1.signal() //本线程(thread2)锁定 condition2.lock() condition2.wait() condition2.unlock() } } print("NSThread 2 over") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }} |
输出结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
NSThread 1 running 0NSThread 1 running 1NSThread 1 running 2NSThread 2 running 0NSThread 2 running 1NSThread 2 running 2NSThread 1 running 3NSThread 1 running 4NSThread 1 running 5NSThread 1 running 6NSThread 1 running 7NSThread 1 running 8NSThread 1 running 9NSThread 1 overNSThread 2 running 3NSThread 2 running 4NSThread 2 running 5NSThread 2 running 6NSThread 2 running 7NSThread 2 running 8NSThread 2 running 9NSThread 2 over |
Swift - 多线程实现方式(1) - NSThread的更多相关文章
- Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...
- iOS开发Swift篇(02) NSThread线程相关简单说明
iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...
- 多线程(一)NSThread
iOS中多线程的实现方案: 技术 语言 线程生命周期 使用频率 pthread C 程序员自行管理 几乎不用 NSthread OC 程序员自行管理 偶尔使用 GCD C 自动管理 经常使用 NSOp ...
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...
- java中创建多线程的方式
在java中比较常用的有三种创建多线程的方式. 方式一:继承Thread类,要重写run方法. 在MyThread类 public class MyThread extends Thread { @O ...
- 实现多线程的方式之实现Callable接口
package com.hls.juc; import java.util.concurrent.Callable;import java.util.concurrent.ExecutionExcep ...
- Java基础加强之并发(二)常用的多线程实现方式
概述 常用的多线程实现方式有2种: 1. 继承Thread类 2. 实现Runnable接口 之所以说是常用的,是因为通过还可以通过JUC(java.util.concurrent)包中的线程池来实现 ...
随机推荐
- AFNetworking 3.0x版本最新特性
AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持. ...
- C#高级编程随笔
1.把类创作的变量叫做对象2.类就是对象的模版3.类定义了每个对象的数据和功能4.接口不能被实例化,抽象类不能被实例化5.抽象基类可以包含非抽象方法,而接口只能包含抽象方法6.一个类可以实现多个接口7 ...
- linux常用命令系列—cp 复制文件与文件夹
原文地址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2272&id=37363 指令名称:cp(copy)功能介绍 ...
- 整数运算:CPU内部只有加法运算
学汇编的一边儿去.我这里讲的是CPU进行计算的原理.首先我这里用MC的红石电路模拟了一个加法器:http://www.0xaa55.com/thread-313-1-1.htm首先加法器是怎么实现的呢 ...
- 流行的Python项目汇总
年有哪些流行的Python项目呢?下面,我们一起来看下. 一.测试和调试 python_koans :Python Koans 算 “Ruby Koans” 的一部分,作为交互式教程,可以学习 TDD ...
- 【开源框架EGOTableViewPullRefresh的研究】
EGOTableViewPullRefresh:点击打开链接https://github.com/enormego/EGOTableViewPullRefresh RootViewController ...
- Shell脚本编程具体解释
第12章 Shell脚本编程 l Shell命令行的执行 l 编写.改动权限和运行Shell程序的步骤 l 在Shell程序中使用參数和变量 l 表达式比較.循环结构语句和条件结构语句 l ...
- uart串口协议
uart串口协议 /* USART Word Length ---------------------------------------------------------*/ US ...
- CF#231DIV2:A Good Number
Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a numbe ...
- 07-IOSCore - CoreData补充、音频视频
xml被plist取代了 数据库被coredata取代了 一.Core Data 高级补充 1. Core Data 本质是什么?操作数据库的数据 ORM Object Relationship M ...